Value types and reference types in C #

Source: Internet
Author: User

Original: Value types and reference types in C #

This article describes the value types and reference types in the C # type system, and some of the differences between them. At the same time, we will introduce boxing and unpacking operations.

Value types and reference types

First, let's look at which types in C # are value types and which are reference types.

Value type:

    • Underlying data type (except String type): Includes integer, float, Decimal, Boolean.
      • Integral type (sbyte, Byte, char, short, ushort, int, uint, long, ULONG)
      • Floating-point type (float and double)
      • Decimal type (decimal)
      • Boolean type (BOOL)
    • struct type (struct)
    • Enum type (enum)

Reference type:

    • Class, interface, delegate, object, String, Array
Default value

There is a default value in the initialization of a variable, in C # we can see the default value of a type by using the defaults keyword.

By default (int), you can see that the defaults for int are 0,default (bool) to show that the default value for the Boolean type is false.

For all reference types, the default value will be null.

Note that there is a special case for struct struct, and if we do the default operation on a struct, we will get the initial value state of each struct member. That is, a value type member assigns a default value to a value type, and a reference type member gives the reference type a default value.

Simple contrast value types and reference types

Below, let's look at a simple example. Suppose there is a point type that has X and y two coordinate members.

Also the following code

New Point (5,9= p1;

If the point type is implemented through a struct struct, then P2 will be a copy of the P1, which means that any modification will not affect the other, and if the point type is implemented through class classes, then P2 and P1 reference values will point to the same object.

To learn more about value types and reference types, we need to introduce the two basic concepts of stacks and heaps.

Stacks and heaps

When we run a program on 32 for the system, this program will have a 4GB process running space. The stacks and heaps we are going to discuss are stored in this 4GB space.

Introduction to stacks and heaps

In C #, stacks (stack) refer to stacks, which are managed heaps and are automatically managed by the. NET garbage collector.

There is no detailed analysis of stacks and heaps, just a simple example to describe how stacks and heaps work.

As you can see, the local variable changes on the stack (into the stack), and when the function is finished, the space on the stack will be cleaned up, but the space we allocate on the heap is always from, only waiting for the GC to help us clean up the space that will not be referenced.

Storage of value types and reference types

After describing the stack and heap, let's look at how value types and reference types are stored.

For a variable of value type, the variable itself represents the value of the value type, but for a variable of the reference type, the instance of the reference type is the space allocated on the managed heap, and the variable itself represents only a reference (pointer) to the managed heap instance.

So here, we can have two summaries of the storage of value types and reference type variables:

    • Reference types are always stored in the heap
    • Value types and references (pointers) are always stored in the heap or stack where they are declared
      • If a value type is not defined in a method, but in a reference type, then this value type will be placed in the reference type and stored on the heap

Note : According to the 2nd summary above, it is wrong to say that the value type must be stored in the stack. For example, we have a student class in which the Age property is a value type, but this value type is stored in the space of the Student class instance, that is, on the heap.

class student{    publicstringgetset;}      Public int Get Set ; }}  
Packing and unpacking

Because all data types in C # are inherited by the base class System.Object, values of value types and reference types can be converted to and from each other through explicit (or implicit) operations.

Here, boxing and unpacking can be described as:

    • Boxing is converting a value type to a reference type
    • Unboxing is converting a reference type to a value type
Internal operation of packing/unpacking

In fact, in the process of packing and unpacking a series of conversions, here is expressed.

When a value type is boxed, a completely new reference object is generated, which can lead to a loss of time, which results in less efficiency. As a result, generics are introduced in C # 2.0 to reduce the cost of boxing and unboxing operations.

Summarize

This article describes the value types and reference types in C #, and the basic concepts of stacks and heaps. The value types and reference types are then analyzed for storage in stacks and heaps.

At the same time, we have learned that:

    • When using reference types, we are dealing with references (pointers) to reference types, not the reference type itself
    • When using value types, we are dealing with the value type itself

Value types and reference types in C #

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.