Go deep into C # chapter 2 of a small part of Data Types,

Source: Internet
Author: User

Go deep into C # chapter 2 of a small part of Data Types,

Value Type and reference type
The value types of C # include struct (numeric type, bool type, user-defined struct type), enumeration, and null type.

C # references include arrays, user-defined classes, interfaces, delegates, objects, and strings.

Array elements, whether of the reference type or value type, are stored on the managed stack.

The reference type stores a reference in the stack, and its actual storage location is located in the managed heap. For convenience, this document deploy the reference type in hosting and pushing.

The value type is always assigned to the place it declares: when it is used as a field, it is stored with the variable (Instance) to which it belongs; when it is used as a local variable, it is stored on the stack.

Value types are more efficient in memory management and do not support polymorphism. They are suitable for storing data. reference types support polymorphism and are suitable for defining the behavior of applications.

Difference between value type and reference type
Each value type has an independent memory region to save its own value. When calling it, it calls its value, while the application type calls the address in memory.
For example, a1 = 18. At this time, 18 is saved in the memory. When a1 is paid to a2, the two applications use the same memory space, the a2 value is saved as the a1 value. When a2 is changed to 20, a1 and a2 reference the same one, so a1 also changes to 20, which is the reference type,
Value Type: When a1 is paid to a2, the a1 value is saved in a new space for a2. When a2 is changed to 20, 20 will be saved in a2 space, and it does not matter with a1.

Value mode parameter transfer
1. Value Parameter)
Format: Method Name (parameter type parameter name [, parameter type parameter name])

2. Reference Parameter)
Format: Method Name (ref parameter type parameter name [, ref parameter type parameter name])

3. output Parameter (Out Parameter)
Format: Method Name (out parameter type parameter name [, out parameter type parameter name])

Ii. Differences between value parameters and reference parameters and output parameters:
2.1 The values of parameters in a value parameter do not change with the value of the parameter;
The values of real parameters do not affect each other. When the real parameters are passed to the parameter, another part of space is allocated to the parameter in the stack. In this way, two parameter changes do not affect each other.
2.2 The values of parameters in reference parameters and output parameters will change as the values of parameters change.
(Parameters: the parameters specified in the definition function do not occupy the storage units in memory when no function call occurs. memory units are allocated only when function parameters are called. after the call is completed, the memory units occupied by the parameters are also released. Assign the value of the real parameter to the callback parameter during the call );
In reference parameters and output parameters, real parameters are allocated space in the stack. When the real parameter is passed to the form parameter, the address is passed to the form parameter, that is, the pointer when the input parameter is changed.

Iii. Differences between referenced parameters and output parameters:
3.1 when using the ref and out parameters, the Ref or out keyword must be added to both the method parameters and call parameters.
3.2 The results obtained by using the ref parameter and the out parameter are the same. The difference is that the ref parameter must be assigned an initial value to the parameter in the call method before calling. Before calling an out method, you do not need to assign an initial value to the parameter in the calling method. The parameter is initialized in the actual method body.

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.