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?
1 Answer
+ 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));
}