I just understood the difference between ref and out today. It is only for my personal understanding. If there are differences, please kindly advise. Thank you.
First of all, I feel that ref and out are for the value type. I used to think that they are for the reference type. Let's look at the following code.
1. First, the result is I = 0; ints [0] = 0 I = 0; ints [0] = 100
2. After ints is passed in as a reference type, ints [0] is assigned a value to reference the address of the reference type to the 100 value stack,
3. When Value Type I is introduced to another method, assigning values does not change the original string.
4. ref is used to solve this problem, so that the value type can be the same as the reference type, and the value will change after passing in the method.
SomeFunction ([] ints,] = Main (I = [] ints = {, ++ ints [++ ints [View Code
5. Add the code to the ref result: I = 0; int [0] = 0; I = 100; ints [0] = 100
SomeFunction ([] ints,] = Main (I = [] ints = {, ++ ints [++ ints [View Code
So I think that ref is a change to the value type, and like out, variables are initialized when passing parameters.
The out parameter does not need to be assigned a value when passing the parameter, but must be assigned a value in the called method. The out parameter that is not assigned is reported.
SomeFunction ([] ints,] = Main ([] ints = {, Console. WriteLine (+ ints [++ ints [View Code