C # differences between reference types and value types

Source: Internet
Author: User

There are two types of 1 *** and C:
Value Type and reference type:
Variables of the value type directly contain their data, while variables of the reference type are stored.
The reference type stores references to their data. The latter is called an object:
Simply put: The value type stores its value directly, and the reference type stores objects that are referenced and referenced.
2 ***,
Value Type: simple type, enumeration, and structure
Reference Type: class type, interface type, array type, and delegate type

3 *** memory storage of value type and reference type
Simply put, the value type is stored on the stack, and the reference type is stored on the managed stack.
4 *. Differences:
1,
Managed heap: Synchronous block and method table, X, Y. GC is required for collection on managed stacks.
Thread Stack: No synchronization block and method table
2,
Value Type assignment creates a new copy.
The value assignment of the reference type shares the same memory (copy) and points to the same memory (the name of the reference type is equivalent to pointing to the operation ). It only copies the reference and does not copy the object identified by the reference. ("Reference" and "referenced object ")
3,
The difference between the value type and the reference type is that the value type variables directly include their data, while the referenced type variables store references in their databases (objects.
4,
All value types are inherited from objects. Any type cannot be derived from the value type. Therefore, the value type is closed.

5 *** when to use the value type
1) when the type is a very simple type, no member will modify any instance field of the type
2) types do not need to be inherited from any other types
3) The type does not derive any other types.
4) Small instances
5) The type is not passed as a method parameter or as the return type of the method.
6) The reference type can contain null values and the value type cannot (the null type function allows null to be assigned to the value type );

6 *** when to use ref and out
Ref: When the value type is passed to be referenced, the ref indicates that the transfer is not a copy, but the same copy.
Out: you do not need to assign an initial value to the variable. If you use out, you do not need to assign an initial value to the outside. The "Initial Value" is assigned internally. The out transfer value is considered uninitialized.

7 *** deep analysis value type and reference type (in-memory deployment)
Example: objectreference r = new object ();
Keyword New: allocate memory space on the managed stack and return the address of the memory space. The reference on the left is located on the stack. It is a reference that stores a memory address, and its content is stored in the memory (on the managed stack) pointed to by this address.

Rule:
1) The reference type is deployed on the managed stack.
2) The value type is always assigned to the place it declares: when it is used as a field, it follows the Instance Storage to which it belongs. As a local variable, it is stored on the stack.
Conclusion: The reference type stores a reference on the stack, and its actual storage location is in the managed heap.

8 *** packing and unpacking operations
1) Packing
Box is the process of converting the value type to the reference type. The opposite process is called Unbox ).
What happened when packing?
(1) allocate memory on the stack. Because the value type eventually has an object, the memory volume allocated on all stacks must be the size of the Value Type plus the amount of memory required to accommodate this object and its internal structure (such as the virtual method table.
(2) The value type is copied to the newly allocated memory.
(3) The recently allocated object address is placed on the stack and now it points to a reference type.

2) binning
Note: Only objects that have been packed in boxes can be split.
You do not need to display the type conversion during packing, but you need to convert the type during unpacking. This is because the object can be converted to any type during unpacking.
Note: the packing operation can be performed implicitly, but the unpacking operation must be displayed.
First, check the object instance to see if it is a boxed value of the given value type.
Then, copy the Instance value to the value type variable.
Try to reduce the number of packing and unpacking operations


9 *** Problem
1) differences between null and "" in C #
String is a reference type:
String STR = NULL: no memory space is created. Str stores null reference pointers.
String STR = "" str stores the pointer to the heap. Point

Simply put:
String STR = "";
Give you a blank sheet of paper;
String STR = NULL;
No white paper.
String. Empty is equivalent to "", which is generally used for string initialization.
Several methods for determining as a Null String, in the order of performance from high to low:
S. Length = 0 is better than S = string. Empty is better than S = ""

2) C # garbage collection
GC (garbage collection) recycles all the code that can be managed, that is, the code in the managed heap.
Generally, a reference variable is defined. It will definitely occupy a space on the heap, and an address on the stack points to this storage location, this memory is recycled only when the GC finds that the stack has no address pointing to it.
Unmanaged resource GC does not automatically recycle, such as database links and files.

Related Articles:

Http://www.cnblogs.com/siqing99/archive/2012/04/03/2430918.html

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.