0

Why the last line in this code do not work?

static void Sqr(ref int x) { x = x * x; } static void Main() { int a = 3; Sqr(ref a); Console.WriteLine(a); // Outputs 9 } ....................................................................... I have tried to write the last line as: Console.WriteLine(Sqr(a)); But I received this error: Argument 1: cannot convert from 'void' to 'bool' why?

6th Jan 2018, 3:00 PM
Muss Mesmari
Muss Mesmari - avatar
1 Réponse
+ 5
Your function didn't return anything so has no value to display. This would work: static int Sqr(ref int x) { x = x * x; return x; } static void Main() { int a = 3; Console.WriteLine(Sqr(ref a)); }
6th Jan 2018, 3:32 PM
John Wells
John Wells - avatar