C # value transfer and reference Transfer

Source: Internet
Author: User

Knowledge point:

Value Type and reference type

For the value type, the data stored in the stack is directly used.

For reference types, the stack stores the address of objects in the stack.


Value Transfer and reference Transfer

For value transfer, the data stored in the stack is transmitted.

For reference transfer, the stack address is passed.


Let's take a look at the value transfer (pass the value type and reference type)

Class program {static void main (string [] ARGs) {// assign a value to the reference type, but assign a value to the address of the object in the heap: person P = new person (); // declare a person class that contains the name attribute p. name = "yzk"; d2 (p); console. writeline (P. name); // return value is JK // Value Type assignment, with a specific value assigned, rather than the address int n = 10; D1 (n); console. writeline (n); // the return value is 10 console. readkey ();} // pass the value of the Value Type Static void D1 (INT m) {M = m + 1 ;} // pass the reference type value static void D2 (person P1) {p1.name = "Jk ";}}
Illustration:


Let's look at the reference transfer (pass the value type and reference type)

Class program {static void main (string [] ARGs) {person P = new person (); // declares a person class, which contains the name attribute p. name = "Jk"; d2 (ref P); // reference transfer: the stack address console is passed. writeline (P. name); // return value SK int n = 10; D1 (ref N); // reference transfer: the stack address is passed to the console. writeline (n); // return value 11 console. readkey ();} // pass the reference of the Value Type Static void D1 (ref int A) // ref indicates that the reference is passed {A = a + 1 ;} // pass static void D2 (ref person per) {per = new person (); Per. name = "SK ";}}
Illustration



C # value transfer and reference Transfer

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.