C # Value types and reference types

Source: Internet
Author: User

value type
attributes of value types:
all value types of 1.c# are implicitly derived from System.ValueType.
each value type and its base class:
structure:struct (directly derived fromSystem.ValueType);
Numeric Type:
Integral type:
ShortSystem.Int16),UShortSystem.UInt16),intSystem.Int32),UIntSystem.UInt32),
LongSystem.Int64),ULongSystem.UInt64),
SByte (alias of System.SByte),ByteSystem.Byte),
Character type:CharSystem.Char);
floating point type:FloatSystem.Single),DoubleSystem.Double);
High-precision decimal type for financial calculations:DecimalSystem.Decimal).
BOOLtype:boolalias of System.Boolean);
enumeration:Enum (derived fromSystem.Enum);
Nullable Types(derived fromsystem.nullable generic struct, syntaxT?It's a shorthand for system.nullable<t>, here.Tto a value type. )
2. Each value type has an implicit default constructor to initialize the default value of the type.
For example:
int i = new int ();
Equivalent to:
Int32 i = new Int32 ();
Equivalent to:
int i = 0;
Equivalent to:
Int32 i = 0;
UseNew operator, a specific type of default constructor is called and the variable is assigned the default value. In the example above, the default constructor values0 was assigned toI
3. All value types are sealed (seal), so new value types cannot be derived.
4.
instances of value types are typically allocated on the thread stack (static allocations), but in some cases can be stored in the heap.
Reference type
Attributes of reference types:
1.
all reference types in C # are implicitly derived from System.Object.
individual reference types and their base classes:
array:(derived fromSystem.Array) The elements of an array, whether they are reference types or value types, are stored on the managed heap;
class:Class (derived fromSystem.Object);
Interface:Interface (the interface is not a "thing", so there is no problem where it is derived.) );
Delegate:Delegate (derived fromSystem.Delegate);
Object :(alias of System.Object);
string: string (the alias of the System.String).
2. A reference type can derive a new type.
3. Reference types can contain null values.
4.
the assignment of a reference type variable copies only references to the object, not the object itself.
5. Objects of reference types are always allocated (dynamically allocated) in the process heap.

Difference between a value type and a reference type
All types that inherit system.value are value types, and other types are reference types.
A reference type can derive a new type, while a value type cannot;
Reference types are stored in the heap, and value types can be stored in the heap or stored in the stack.
A 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);
The assignment of a reference type variable copies only references to the object, not the object itself. When you assign a value type variable to another value-type variable, the contained value is copied.
When comparing two value types, there is a comparison of the content, whereas a reference comparison is made when comparing two reference types.
Value types are more efficient in memory management and do not support polymorphism, and are suitable as vectors for storing data; reference types support polymorphism and are suitable for defining the behavior of an application.
Int[] is a reference type or a value type
array types are family types, all of which inherit System.Array, and System.Array inherit from System.Object. So all array types are reference types

C # Value types and reference 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.