+ 1

How is the answer 30 ?

What is the output of this code? static int Test(out int x, int y=4) { x = 6; return x * y; } static void Main(string[] args) { int a; int z = Test(out a); Console.WriteLine(a + z); } //output 30

26th Feb 2018, 7:24 PM
Zahid Farook
Zahid Farook - avatar
2 Respostas
+ 9
out makes 'a' be passed by reference and does not require the variable to have an initial value. The value can be set in the method that the variable is being passed to. This is why: int a; Test(out a); Does not cause issues, even though 'a' has not been given any value. x = 6 also makes 'a' equal to 6 since 'a' was passed by reference. x * y is 6* 4 = 24. z is 24 since 24 is returned. x + a = 6 + 24 = 30.
26th Feb 2018, 9:48 PM
Rrestoring faith
Rrestoring faith - avatar
0
thaks a lot
15th Aug 2020, 10:55 PM
-sh1r0
-sh1r0 - avatar