Correct understanding of the ref keyword in C #

Source: Internet
Author: User

Recently, someone asked about the correct usage of the ref keyword. The following is an example. In fact, we need to better understand the ref keyword, combined with C ++CodeIt is easier. In addition, before starting our example, we need to explain the following points in advance:

    • There are two types of data in C #: reference types and value types ).
      Simple types (including int, long, double, etc.) and structures (structs) are both value types, while other classes are reference types.
      A simple type performs a copy operation when passing values. A reference type only transmits a reference, just like a pointer in C ++.
    • Note the differences between structs in C # And C ++. In C ++, structs is basically the same as Class (except that the default inheritance and default access are public rather than private ).
      In C #, structs differs greatly from classes. The biggest difference (I personally think it is easy to ignore) is that it is a value type, not a reference type.

The following code is an example in msdn:

// Cs_ref.csusing system; public class myclass {public static void testref (ref char I) {// The value of I will be changed in the calling method I = 'B ';} public static void testnoref (char I) {// The value of I will be unchanged in the calling method I = 'C ';} // This method passes a variable as a ref parameter; the value of the // variable is changed after control passes back to this method. // The Same variable is passed as a value parameter; the value of the // variable is unchanged after control is passed back to this method. public static void main () {char I = 'a'; // variable must be initialized testref (Ref I); // The ARG must be passed as REF console. writeline (I); testnoref (I); console. writeline (I );}}

The output result is as follows:

 
Bb

If you make some new changes to this example, change the value type (char is used here) to the reference type,ProgramWhat is the effect of running?

 
// -------------------------------------- // Myclass definitionpublic class myclass {public int value;} // define // tester methodspublic static void testref (ref myclass m) {M. value = 10;} public static void testnoref (myclass m) {M. value = 20;} public static void testcreateref (ref myclass m) {M = new myclass (); M. value = 100;} public static void testcreatenoref (myclass m) {M = new myclass (); M. value = 200;} public static void main () {myclass M = new myclass (); M. value = 1; testref (ref m); console. writeline (M. value); testnoref (m); console. writeline (M. value); testcreateref (ref m); console. writeline (M. value); testcreatenoref (m); console. writeline (M. value );}

Can you give the correct answer immediately? If you can, it seems that you have a good understanding of ref usage. In fact, if you are familiar with C ++, you can simply replace this code with C ++.

// ---------------------------------------- // Myclass definition # pragma onceclass myclass {public: int value ;}; Typedef myclass * myclassptr; // ---------------------------------------- // Tester methodsvoid testref (char * I) {* I = 'B';} void testnoref (char I) {I = 'C ';} void testref (myclassptr * m) {(* m)-> value = 10;} void testnoref (myclassptr m) {M-> value = 20 ;} void testcreateref (myclassptr * m) {Delete (* m); * m = new myclass (); (* m)-> value = 100;} void testcreatenoref (myclassptr m) {M = new myclass (); m-> value = 200;} int main (INT argc, char * argv []) {char c = 'a '; testref (& C); printf ("% C \ n", c); // output: B testnoref (c); printf ("% C \ n", C ); // output: B myclassptr M = new myclass; m-> value = 1; testref (& M); printf ("% d \ n", M-> value ); testnoref (m); printf ("% d \ n", M-> value); testcreateref (& M); printf ("% d \ n ", m-> value); testcreatenoref (m); printf ("% d \ n", M-> value); Delete m; return 0 ;}

The output results of the codes implemented using C # And C ++ are the same. The output result of myclass testing is as follows:

 
1020100100

The specific reasons are believed to be clear after analysis. If you are interested, you can try structs again, or you can further understand the differences between structs in C ++ and C.

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.