+ 1

Why the output is 2? چرا خروجی این کد ۲ می شود؟

Please explain me step by step, what happens to this code and why the output is 2. thanks لطفا برای من توضیح دهید که در این کد قدم به قدم چه اتفاقی می افتد و چرا خروجی ۲ می شود؟ سپاس https://code.sololearn.com/cK7PDnuYNK9A/?ref=app

30th Dec 2017, 7:59 AM
الف.پ
الف.پ - avatar
2 Answers
+ 11
Taking an extract of the code: static void Main(string[] args) { int a=0; a++;// This line increases a to 1 این خط به 1 افزایش می یابد var res=inc(a++); // The method inc() takes 1 as a parameter not two as you might assume(a++ is a postfix operator meaning the old value is first used before it is incremented). The inc() method returns its parameter plus 1 so it returns 1 + 1 = 2 روش inc () 1 به عنوان پارامتر نه دو مورد را فرض می کند (یک ++ یک اپراتور postfix است که به معنی استفاده از مقدار قدیمی است قبل از افزایش آن). روش inc () پارامتر خود را به علاوه 1 باز می گرداند، بنابراین 1 + 1 = 2 باز می گردد Console.WriteLine(res++);//finally res is 2 سرانجام res 2 است } static int inc(int arg) {return arg+=1;}
30th Dec 2017, 8:45 AM
David Akhihiero
David Akhihiero - avatar
+ 1
سپاس thank you
30th Dec 2017, 9:02 AM
الف.پ
الف.پ - avatar