Value Type and reference type. Value Type reference type

Source: Internet
Author: User

Value Type and reference type. Value Type reference type

C # has three types of data:

1. Value Type

2. Reference Type

3. pointer type (for non-secure code only)

Value Type-- Directly derived from System. ValueType

1. structure type (struct)

1. Simple Type

1. Integer

INTEGER (byte -- System. Byte, sbyte -- System. SByte,

Long -- System. Int64, ulong -- System. Int64,

Short -- System. Int16, ushort -- System. Int16,

Int -- System. Int32, uint -- System. Int32)

Float Type (float -- System. Single, double -- System. Double)

Decimal -- System. Decimal

2. character type (char) -- System. Char

3. Boolean type (bool) -- System. Boolean

2. User-defined structure type

2. Enumeration type (enum) -- derived from System. Enum

3. null type -- derived from System. Nullable generic struct

Features:
1. All value types of C # are implicitly derived from System. ValueType.
2. Each value type has an implicit default constructor to initialize the default value of this type.
3. All value types are seal, so a new value type cannot be derived.
4. Value-type instances are usually allocated on the thread stack (static allocation), but in some cases they can be stored in the heap.

Reference Type-- Implicitly derived from System. object

1. Class

User-Defined class)

Object base class

String class (String) -- System. string

2. interface-the interface is not a "thing", so there is no problem where it is derived

3. array (Array) -- System. array

4. delegate-proxy (Delegate) -- System. delegate)

Features:
1. All reference types of C # are implicitly derived from System. object.
2. The reference type can be derived from a new type.
3. The reference type can contain null values.
4. The value assignment of the reference type variable only copies the reference to the object, instead of copying the object itself.
5. Objects of the reference type are always allocated in the process heap (Dynamic Allocation ).

Summary:

1. All types that Inherit System. Value are Value types, and Other types are reference types.
2. 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 );
3. The value assignment of the reference type variable only copies the reference to the object, instead of copying the object itself. When a value type variable is assigned to another value type variable, the included values are copied.
4. When comparing two value types, compare the content, and compare the reference types.
5. value types have better efficiency 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.
6. the Array type is a family type. They all Inherit System. Array, while System. Array inherits from System. Object. Therefore, all array types are reference types. Array elements, whether of the reference type or value type, are stored on the managed stack.

The essential differences between the value type and the reference type are as follows::

1. Memory Allocation: The value type can be stored in both the heap and stack, and the reference type is stored in the managed stack. The value type is directly taken from the stack, and the reference type must first retrieve its address from the stack, and then find the corresponding value in the stack based on this address.
2. Efficiency: the value type efficiency is high, and address conversion is not required; the reference type efficiency is low, and address conversion is required.
3. Memory reclaim: The value type is recycled immediately after use. The reference type is not recycled immediately after use, but is handed to GC for recovery.
4. Value assignment operation: the value type is to create a new object, and the reference type creates a reference.
5. type Extension: The value type is not easy to expand. All value types are seal, so new value types cannot be derived. The reference type can be derived from new types with polymorphism, easy to expand.

 

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.