C # Reference types and value types _c# Tutorial

Source: Internet
Author: User
Tags abstract garbage collection
The CLR supports two types: reference types and value types.
Reference types are always allocated from the managed heap.

The new operator in C # returns the memory address of the object.

Note Points for referencing objects:

1. Memory allocated from the managed heap
2, the heap on the allocation of objects, there are some additional operations, affecting some of the performance of
3. When assigning an object from the managed heap, a garbage collection may be enforced.

Value types in the CLR are lightweight. You can reduce the number of garbage collections without requiring a pointer to be raised and no garbage collection.
Generally referred to as "classes" in the CLR are reference types, all value types are called structs or enumerations.
All structures are directly derived classes that are abstract classes valuetype. The valuetype is derived directly from object.
All enumerations are derived from the Enum abstract class, and the enum is derived from ValueType.

A value type can implement one or more interfaces, but it cannot inherit the base class. All value types are implicitly sealed.

C # uses struct to declare structs, class to declare classes.

Value types should be satisfied:

1, type has primitive type
2. Type does not need to inherit from any other type
3, not as a base class
4, the type of size do not exceed 16k

Different points for value types and reference types:

1. You cannot use a value type as a base type
2, the reference type variable contains an object address on the heap. The value type is the value object itself.
3, the value type assignment will perform a verbatim paragraph copy, referencing the type variable assignment, only the memory address copied.
4. Multiple variables of a reference type may reference the same object in the heap, and the value type is self-contained and does not affect each other.
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.