I thought I understood the difference, but now I'm not so sure. I've read the technical answer several times but I'm not understanding what is happening. I have this example.
class Program
{
static void Main()
{
int val = 0;
Example1(val);
Console.WriteLine(val); // Still 0!
Example2(ref val);
Console.WriteLine(val); // Now 2!
Example3(out val);
Console.WriteLine(val); // Now 3!
}
static void Example1(int value)
{
value = 1;
}
static void Example2(ref int value)
{
value = 2;
}
static void Example3(out int value)
{
value = 3;
}
}
I always thought the difference between default parameters is that if I am passing val into Example1, I can't use assignment.
But with the ref keyword val is still 0, but I have created a reference that is now treated as the variable "value" within Example2(ref val). Am I getting hung up on anything so far? if I had used
int value = 0;
Example1(value);
Console.WriteLine(value); // this would then return 1 correct?
Now then, what is happening with the out keyword? Same thing as ref?