The difference between ref and out is not mentioned. This is what I search online.
I want to say one thing: many people say that ref is meaningless to the reference type. I think this sentence is entirely based on my own imagination and I will not go through the book.
In the original article of MSDN, "refThis allows the called method to modify the object referenced by the reference, because the reference itself is passed by reference. The following example shows that when the reference type isRefWhen the parameter is passed ,."
Here is a string example. I personally think this example is not good, because the string itself is a special reference type. The example is as follows:
Class RefRefExample {static void Method (ref string s) {s = "changed";} static void Main () {string str = "original"; Method (ref str ); // str is now "changed "}}
I also did a small test on this feature of ref. The Code is as follows:
Protected void Page_Load (object sender, EventArgs e) {myclass ACC = new myclass (); ACC. id = 1; py. name = "shit"; Response. write (ACC. id + "<br>" + ACC. name + "<br>"); // The reference object without the ref original variable, which does not change ModifyMyclass (ACC); Response. write (ACC. id + "<br>" + ACC. name + "<br>"); // The reference object with the ref original variable. The new object ModifyMyclass (ref ACC); Response is referenced. write (ACC. id + "<br>" + ACC. name + "<br>");} private void ModifyMyclass (myclass ACC) {py = new myclass (); py. id = 23; py. name = "new myclass";} private void ModifyMyclass (ref myclass ACC) {ACC = new myclass (); ACC. id = 23; py. name = "ref new myclass ";}
Deliberately wrote a myclass class as a parameter for passing
public class myclass{ private int id; public int Id { get { return id; } set { id = value; } } private string name; public string Name { get { return name; } set { name = value; } }}
Print result:
1
Shit
1
Shit
23
Ref new myclass
Obviously, ref is meaningful to the reference type.
Therefore, I also want to think about it: I don't know if this understanding is correct, but I still forget to analyze it in depth!