Difference between value type and reference type (based on C #)

Source: Internet
Author: User

Differences between value types and reference types (based on C #) 1. Type Distribution C # The value types include the following types: simple type, structure type (struct), and enumeration type (enum ). Simple types include: INTEGER (int), Boolean (bool), character (char), and real (double, decimal). Needless to say about the structure and enumeration, the structure is the sub-assembly used to store small variable groups. You can understand enumeration. The reference type in C # contains the following types: Class, interface, array, and delegate, which all belong to the reference type. 2. the C # value type is stored as a value, and the value type is usually allocated to the stack. In C #, instances of the reference type are allocated to the stack. Create a new instance of the reference type. The variable value corresponds to the memory allocation address of the instance, just like your bank account. 3. demousing System; using System. collections. generic; using System. text; namespace ConsoleApplication1 {class Person {public int Blood = 10;} class Program {public static void Add (int x) {x ++ = 10; Console. writeLine ("value type after the parameter is passed and modified:" + x);} public static void Add (Person person) {person. blood + = 10; Console. writeLine ("reference type after the parameter is passed and modified:" + person. blood);} static void Main (string [] args) {// Value Type Variable int I = 10; Console. writeLine ("I Original Value:" + I); Add (I); Console. writeLine ("but the I value is not modified because of Function Modification:" + I); // reference type variable Person person = new Person (); Console. writeLine ("original Blood value:" + person. blood); Add (person); www.2cto.com Console. writeLine ("but the value of Blood is modified because of the Function Modification:" + person. blood); // The difference between the value type and the reference type is that when function parameters are passed. // The value type is to copy and pass your own values to other function operations. no matter how the copied value is changed. its own value will not change // and the reference type is to pass its memory address to other function operations. the operation is the reference type value itself. so the value is changed by the function. // This is the difference between data transfer and data transfer. Console. readLine ();}}}

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.