I don't understand why my program is outputting the way it is
I've written a program to demonstrate using the keyword ref to use pass-by-reference. I'm expecting this to be output:
The value of x is 5
The value of product is 25
The value of x is 25
But instead my program is showing this:
The value of x is 5
The value of product is 25
The value of x is 5
It does not appear that the final value of x in the Main method is being changed like it should be. Here is my code:
Code:
using System;
class PassByReference
{
static void Main()
{
int x = 5;
int product;
Console.WriteLine("Value of x is: " + x);
product = Multiply(ref x);
Console.WriteLine("Value of product is: " + product);
Console.WriteLine("Value of x is: " + x);
Console.WriteLine();
}
static int Multiply(ref int a)
{
return a * a;
}
}