Scripts of ref

Source: Internet
Author: User

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!

 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.