C # value type and reference type,

Source: Internet
Author: User

C # value type and reference type,

This article aims to consolidate the basic knowledge, not to conduct in-depth analysis on it, but also hope to understand it.
This article is a summary of this knowledge. Some of the content comes from the Internet, and others are original bloggers, so there will inevitably be some flaws. Please forgive me.
All examples have been tested by the bloggers. If any examples are reposted, indicate the source. Thank you.

The value type and reference type are very basic knowledge, and there are also many introductions to this. If you want to learn more, instructor Zhang's blog is very helpful. The link is as follows:
Http://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.html
Here is a brief introduction:
Conceptually, the value type directly stores its value, while the reference type stores its reference to its value.The value type is cash, which should be used directly; the reference type is passbook, which must be obtained from the bank first.

We know that each type in C # is either a value type or a reference type. Therefore, each object is either a value-type instance or a reference-type instance.
Base class of value type and reference type
Both the reference type and value type inherit from the System. Object Class. The difference is that almost all reference types are inherited directly from System. Object, while the value type inherits its subclass, that is, the System. ValueType type is inherited directly.
As a base class of all types, System. Object provides a set of methods that can be found in all types, including the toString method and clone method.
System. valueType directly inherits System. object, that is, System. valueType itself is a class type, not a value type; System. valueType does not add any Members, but overwrites some inherited methods to make it more suitable for value types. For example, ValueType overrides the Equals () method to compare the value type by instance value rather than the reference address.

struct Program    {        static void Main(string[] args)        {            Program testType = new Program();            if (testType.GetType().IsValueType)            {                Console.WriteLine("{0} is value type.", testType.ToString());            }            Console.ReadLine();        }    }

Value Type
Value Type features:
1. All value types of C # are implicitly derived from System. ValueType.
Each value type and its base class:
Struct:Struct (directly derived from System. ValueType );
Value Type:
Integer:
Short (System. Int16), ushort (System. UInt16), int (System. Int32), uint (System. UInt32 ),
Long (System. Int64), ulong (System. UInt64 ),
Sbyte (alias of System. SByte), byte (System. Byte ),
Character Type:Char (System. Char );
Floating point type:Float (System. Single), double (System. Double );
High-Precision decimal type for financial computing: Decimal (System. Decimal ).
BoolType: Bool (alias of System. Boolean );
Enumeration:Enum (derived from System. Enum );
Null type(Derived from System. Nullable generic struct, syntaxT?Is short for System. Nullable <T>.TIs the value type .)
2. Each value type has an implicit default constructor to initialize the default value of this type.
For example:
Int I = new int ();
It is equivalent:
Int32 I = new Int32 ();
It is equivalent:
Int I = 0;
It is equivalent:
Int32 I = 0;
When the new operator is used, the system calls the default constructor of a specific type and assigns the default value to the variable. In the preceding example, the default constructor assigns the value 0 to I.
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
Features of the reference type:
1.
All reference types of C # are implicitly derived from System. object.
Each reference type and its base class:
Array:Elements of the Array (derived from System. Array) are stored on the managed stack, regardless of the reference type or value type;
Class:Class (derived from System. Object );
Interface:Interface (the interface is not a "thing", so there is no problem where it is derived .);
Delegate:Delegate (derived from System. Delegate );
Object:(Alias of System. Object );
String:String (alias of System. String ).
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 ).

Difference between value type and reference type
All types that Inherit System. Value are Value types, and Other types are reference types.
The reference type can be derived from a new type, but the value type cannot;
The reference type is stored in the heap, while the value type can be stored in both the heap and stack.
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 );
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.
When comparing two value types, content comparison is performed, and when comparing two reference types, reference comparison is performed.
Value types are more efficient 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.
Whether Int [] is a reference type or a value type
The Array type is the reference type. Both of them inherit System. Array, and System. Array inherit from System. Object. Therefore, all array types are reference types.

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.