Today I just understand that the difference between ref and out is limited to personal understanding. Please enlighten me if you have different.
First I feel that ref and out are for value types, which was previously thought to be for reference types see the following section of code
1. First results i=0;ints[0]=0 i=0;ints[0]=100
2.ints as a reference type after passing the method, the ints[0] is assigned, is to refer to the address of the type of the 100 is worth the heap,
3. When the value type I is introduced to another method, the assignment does not change the original string
4.ref is to solve this problem, so that the value type can be the same as the reference type after the incoming method value will change
Using system;using system.collections.generic;using system.linq;using system.text;namespace Demo{ class Program { static void SomeFunction (int[] ints, int i) { ints[0] = n; i = +; } static void Main (string[] args) { int i = 0; Int[] INTs = {0, 1, 2, 3, 4}; Console.WriteLine ("i=" + i); Console.WriteLine ("ints=" + ints[0]); SomeFunction (INTs, i); Console.WriteLine ("i=" + i); Console.WriteLine ("ints=" + ints[0]); Console.readkey ();}}}
5. Add code to ref result: i=0;int[0]=0;i=100;ints[0]=100
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Demo
{
Class Program
{
static void SomeFunction (int[] ints, ref int i)
{
Ints[0] = 100;
i = 100;
}
static void Main (string[] args)
{
int i = 0;
Int[] INTs = {0, 1, 2, 3, 4};
Console.WriteLine ("i=" + i);
Console.WriteLine ("ints=" + ints[0]);
SomeFunction (ints, ref i);
Console.WriteLine ("i=" + i);
Console.WriteLine ("ints=" + ints[0]);
Console.readkey ();
}
}
}
So I think that ref is a change to the value type and, like out, the variables are initialized when the arguments are passed.
The out-of-the-box does not have to be assigned when passing parameters, but it must be assigned in the calling method, which will report out parameters that are not assigned.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Demo
{
Class Program
{
static void SomeFunction (int[] ints, ref int i)
{
Ints[0] = 100;
i = 100;
}
static void Main (string[] args)
{
int i = 0;
Int[] INTs = {0, 1, 2, 3, 4};
Console.WriteLine ("i=" + i);
Console.WriteLine ("ints=" + ints[0]);
SomeFunction (ints, ref i);
Console.WriteLine ("i=" + i);
Console.WriteLine ("ints=" + ints[0]);
Console.readkey ();
}
}
}
The difference between ref and out, the use of value types and reference types