Reference Type and Value Type

Source: Internet
Author: User

Excerpt:

Thread stack and thread heap: both are in the memory area. value types are allocated on the stack [post-import, first-out]; reference type variables are allocated on the stack, and content is allocated on the stack;

1. Value Type

A. value types include structure and enumeration, and reference types include class, interface, and delegate. There is also a special value Type called Simple Type, such as byte and int.

B. all value types are implicitly inherited from System. valueType type (note System. valueType itself is an abstract class), so it cannot be displayed that one value type (structure) can inherit another class, because c # does not support multiple inheritance, while System. valueType and all reference types are inherited from System. object base class,

C. When a Variable of the value type is declared, the Variable itself contains all fields of the value type, and the Variable will be allocated on the Thread Stack.

D. the compiler implicitly creates a non-parameter constructor for the structure type. In this constructor, structure members are initialized. All value type members are assigned 0 or a value equivalent to 0 (for Char type). All reference types are assigned null values. (Therefore, the Struct type cannot declare a constructor without parameters ). Therefore, we can create a ValPoint type variable using the implicitly declared constructor:

Public struct ValPoint
{
Public int x;
Public ValPoint (int x)
{
This. x = x;
}
}

ValPoint vPoint1 = new ValPoint (); Console. WriteLine (vPoint. x); // The output is 0. We split the expression in the first sentence of the above Code into two parts separated by "=:

• ValPoint vPoint1 on the left creates a ValPoint variable vPoint on the stack. No value is assigned to all the members of the structure. Press the vPoint to the stack before performing the new ValPoint () operation.

• The new ValPoint () operator on the right does not allocate memory. It only calls the default constructor of the ValPoint structure and initializes all fields of the vPoint structure according to the constructor.

Note that the new operator does not allocate memory. It only calls the default constructor of the ValPoint structure to initialize all vPoint fields. How can I explain this?

Console. WriteLine (new ValPoint (). x); // normal, output is 0

// In this case, a temporary variable is created and initialized using the default constructor of the structure.

 

2. Reference Type

Public class RefPoint
{
Public int x;
Public RefPoint (int x)
{
This. x = x;
}
Public RefPoint (){}
}

When we write only one declaration statement:

RefPoint rPoint1; // It only creates a variable on the stack that does not contain any data or point to any object (does not contain the address of the object created on the stack.

When we use the new operator:

RPoint1 = new RefPoint (1 );

This will happen:

1. Create an Instance or Object on the application Heap and allocate the memory address to it.

2. automatically pass the instance reference to the constructor. (Because of this, you can use this in the constructor to access this instance .)

3. Call this type of constructor.

4. Return the instance reference (memory address) and assign the value to the rPoint variable.

 

////////// Summary value parameter, reference type parameter, output parameter ///////////////// //////////

1. Value Parameters

When the method is called, The system performs the following operations:

A. Allocate space for the form parameter in the stack

B. Copy the value of the real parameter to the form parameter, so that the change of the value in the form parameter will not affect the value in the original real parameter, but only a copy of the original real parameter value.

------------ Real parameters are not necessarily variables. They can be any expressions that can be computed as corresponding data types.

2. Reference parameters

The ref modifier must be used in the method declaration and call. The real parameter must be a variable and must be assigned a value before being used as a real parameter. If it is a reference type variable, it can be assigned a reference or null value.

A. do not allocate new memory for the form parameter in the stack

B. The name of the parameter is equivalent to the alias of the real variable, and the same memory location is referenced as the real parameter.

3. Output Parameters

It is used to transmit data from the method body to the calling code. They are very similar to reference parameters.

A. the out modifier must be used in all declared calls.

B. The real parameter must be a variable and cannot be of another expression type.

C. do not allocate new memory for the form parameter in the stack

D. The name of the parameter is equivalent to the alias of the real variable, and the same memory location is referenced as the real parameter.

E. In the method, the output parameter must be assigned a value before being read.

F. Each output parameter must be assigned a value before returning the method.

4. Parameter Array

It allows zero or multiple real parameters to correspond to a special form parameter.

A. You can only have one parameter array in the parameter list.

B. If yes, it must be the last one in the list.

Eg: void ListInts (params int [] inVals ){}

 

 

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.