Example: C # ref and out

Source: Internet
Author: User
From this field, the ref indicates the reference. out is the output.

Public void getrefstr (Ref String Str) {STR = "Hello, you have changed to ref "}
Public void getoutstr (Out String outstr) {outstr = "Hello, you are the output value of out ";}
 
Protected void button#click (Object sender, eventargs e) {string Ss = "hello"; getstr (ref SS); response. write (SS); // output ref reference value} protected void button2_click (Object sender, eventargs e) {string outvalue; Method (Out outvalue); response. write (outvalue); // output out value}

The previous image below

Click ref.

Click out

See the instance andCodeNow let's talk about ref and outstring Ss = "hello"; // SS is actually a reference SS ---------> "hello" getstr (ref SS );//Ref SS indicates the getstr (ref string Str) STR method. Buddy, your reference address is ss.Public void getrefstr (ref string Str) {STR = "Hello, you have changed to ref" // reference the main method in the new} ref button {Ss = "hello "; getstr (ref SS); response. write (SS); // so when the value of the input SS changes, because its reference address has changed}

 
Public void getoutstr (out string outstr) {outstr = "Hello, you are the output value";} out button {string outvalue ;//No null value. Reference outvalue ---------> emptyMethod (Out outvalue); // input reference others' outstr = "Hello, you are the output value of out"; response. write (outvalue); // The output out value changes accordingly}

In the final conclusion, both out and ref are actually the actual reference address, so its value also changes. The previous simple address reference diagram

Each reference opens up memory space.

 







Related Article

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.