Differences between C # Reference types and value types

Source: Internet
Author: User

1***, there are two types in C #: Value types and reference types: variables of value types directly contain their data, whereas variable storage reference types for reference types store references to their data, which are called objects: Simply put: value types store their values directly, and reference types store pairs that are worth referencing. Reference types are categorized as references and references. Object. 2***, Value type: Simple Type, enum, struct reference type: class type, interface type, array type, and delegate type3***, value types and reference types of memory storage simply say value types are stored on the stack, and reference types are not stored on the managed heap. 4***, Difference:1, managed heap: Synchronous block and Method table, x, Y, required GC back-and-forth thread stacks on the managed heap: No synchronization blocks and method tables2, the value type assignment is to recreate a copy and the assignment of the reference type is to share the same block of memory (the copy), pointing to the same block of memory (the name of the reference type is equivalent to the pointing operation). Simply copy the reference without copying the object that is referenced. ("References" and "referenced objects")3, value types differ from reference types in that variables of value types directly include their data, whereas variables of reference types store references in their database (Objects). 4, all of the value types inherit from the object.   No type can be derived from a value type, so the value type is closed. 5***when to use value types1when a type is a very simple type, where no member modifies any instance field of the type2) type does not need to inherit from any other type3) type does not derive from any other type4) Instances of the type are small5) type is not passed as a method parameter, nor is it used as the return type of the method6A reference type can contain a null value, and a value type cannot (a nullable type function allows NULL to be assigned to a value type);6***when to use ref and outref: When a value type is passed to a reference, ref is telling the passing time that it is not a copy, but the same copy.  out: Do not need to assign the initial value to the variable, use out also does not need to assign the initial value outside, is in the internal assigned "initial value", the out transfer values are understood as not initialized. 7***depth Analysis value types and reference types (in-memory deployments) Example: Objectreference R=New Object(); The keyword new allocates memory space on the managed heap and returns an address for that memory space.    The reference on the left is on the stack, is a reference, stores a memory address, and the memory (located on the managed heap) that the address points to stores its contents. Rule:1) Reference type is deployed on the managed heap2The value type is always assigned where it is declared: As a field, follow the instance store to which it belongs.     Stored on the stack as a local variable. Summary: A reference type stores a reference on the stack whose actual storage location is in the managed heap. 8***packing and unpacking operations1Boxing (box) is the process of converting a value type to a reference type.       The opposite process is called unpacking (unbox). What happened when the box was boxed?           (1) to allocate memory on the heap.           Because the value type eventually has an object represented, the amount of memory allocated on all the heaps 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. (2Value of the value type is copied to the newly allocated memory (3the newly allocated object address is placed on the stack, and now it points to a reference type. 2Unpacking Action Note: Objects that have been loaded in the box can be removed from the box.       There is no need to display type conversions when boxing. However, type conversions are required when unpacking. This is because objects can be converted to any type when unpacking.       Note: The boxing operation can be performed implicitly, but the unpacking operation must be displayed.       First, examine the object instance to see if it is a boxed value of the given value type.   Then, copy the value of this instance to a variable of the value type. To minimize packing and unpacking operations9***problem1) in C # null and""The difference string is a reference type:stringStr=NULLNo memory space is created, and a null reference pointer is stored in STR. stringStr=""A pointer to the heap is stored in Str. There are points to simply say:stringstr =""; Give you a blank sheet of paper;stringstr =NULL;    Not even white paper. string. Empty is equivalent to""generally used for the initialization of the string is determined to be an empty string of several ways, according to the performance from high to low order is: S. Length==0Better than S = =string. Empty better than S = =""       2C # garbage collection GC (garbage collection) garbage collection is managed code, that is, the code in the managed heap generally defines a reference variable, it will certainly occupy a space on the heap, there is an address on the stack to point to this storage location, only the party GC found on the heap this resource on the stack no address pointing to it      Time to recycle this piece of memory. Unmanaged Resources GC is not automatically recycled, such as database links, files, and so on. 

Differences between C # Reference types and value types

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.