If we change the integer argument of the call method for Fact into 10, what happens? Explain your answer
Integer type is value type, so when we pass it to some method as argument, it will copy just a value, but no a reference. It means that any changes inside the method will not affect real value that was passed. To change value-type into the method we have to use ref/out keywords.
static void Main(string[] args)
{
int someValue = 20;
ChangeSomething(someValue);
Console.WriteLine($"Value = {someValue}"); //20
ChangeSomething(ref someValue);
Console.WriteLine($"Value = {someValue}"); //10
Console.ReadLine();
}
public static void ChangeSomething(int value)
{
value = 10;
}
public static void ChangeSomething(ref int value)
{
value = 10;
}
Comments
Leave a comment