Discussion on Value Type reference type and value transfer reference Transfer

Source: Internet
Author: User

I kept chatting with my colleagues, so I checked the document, reviewed the related value types and reference types, value transfer and reference transfer, and summarized and shared them! If any error occurs, correct it! A person has limited thoughts, brainstorming, and common development.

Value Type and reference type

First, distinguishC #Difference between the value type and the reference type in:

Value types include:Int,Char,Double,Float,Long,Short,Byte,Bool,Enum,Struct,Decimal.

All value types are derived fromValueTypeIs an abstract class

The value type cannot be inherited, and only interfaces can be implemented.

② String, Array, class (custom data type), interface, Delegate,

Int [] n = {1, 2, 3 };//Reference type.

The reference types are derived from:Object

The reference type can be inherited (between classes can be inherited)

Rules that need to be known and understood

1Remember to declare variables on the stack

2After the value type declares the variable, no matter how many values are mostly small, the variable size is fixed and there is a stack

3, After referencing the type declaration variable, it depends on the value! The size is not fixed and stored on stacks and stacks.

The value transfer case is as follows:

Class Program {static void Main (string [] args) {// 1. value Type = // int n = 10; // int m = n; // m ++; // Console. writeLine (n); // Console. readKey (); // 2. reference Type ========/// Person p = new Person (); // p. age = 17; // p. name = "johnson"; // p. email = "johnsondzd@qq.com"; // Person p1 = p; // p1.Age = p. age + 12; // Console. writeLine (p. age); // Console. readKey (); // case 1 // int m = 1; // M1 (m); // when the value type is passed as a parameter, a copy is copied ], therefore, changing the copy does not affect the original content // Console. writeLine (m); // Console. readKey (); // case 2 // int [] arrInt = {1, 3, 5, 7, 9,}; // M2 (arrInt ); // for (int I = 0; I <arrInt. length; I ++) // {// Console. writeLine (arrInt [I]); //} // Console. readKey (); // case 3 // int [] arrInt = {1, 3, 5, 7, 9,}; // M3 (arrInt ); // for (int I = 0; I <arrInt. length; I ++) // {// Console. writeLine (arrInt [I]); //} // Console. readKey (); // case 4 // Person p = new Person (); // p. name = "johnson"; // p. age = 16; // M4 (p); // Console. writeLine (p. name); // Console. writeLine (p. age); // Console. readKey (); // case 5 Person p = new Person (); p. name = "johnson"; p. age = 16; M5 (p); Console. writeLine (p. name); Console. writeLine (p. age); Console. readKey ();} static void M5 (Person per) {per = new Person (); per. name = "vinson"; per. age = 32;} static void M4 (Person per) {per. name = "vinson"; per. age = 32;} static void M3 (int [] arr) {int [] arr1 = {2, 4, 6}; arr = arr1; for (int I = 0; I <arr. length; I ++) {arr [I] = arr [I] + 2 ;}} static void M2 (int [] arr) {for (int I = 0; I <arr. length; I ++) {arr [I] = arr [I] + 2 ;}} static void M1 (int n) {n = 10; n ++ ;}} class Person {public string Name {get; set;} public int Age {get; set;} public string Email {get; set ;}}

 

Reference transfer case

Class Program {static void Main (string [] args) {// value transfer: Value Type, reference type // reference transfer: value Type, reference pass ref // 1, reference pass is value type // int n = 10; // M1 (ref n); // Console. writeLine (n); // Console. readKey (); // 2. The reference is passed by the reference type Person p = new Person (); p. name = "johnson"; M2 (ref p); Console. writeLine (p. name); Console. readKey ();} private static void M2 (ref Person p) {p = new Person (); p. name = "vinson";} // ref indicates "reference transfer". The reference transfer is not the data in the stack (for value type, the data in the stack is saved directly; for reference types, the stack stores "object address", but static void M1 (ref int m) {m ++ ;}} class Person {public string Name {get; set;} public int Age {get; set;} public string Email {get; set ;}}

Author: WM/Johnson

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.