Value types & Reference types, boxing & unpacking

Source: Internet
Author: User

Value type: Declares a value type variable, allocates a space on the stack, the value of the variable is stored in the space.
Reference type: Declares a reference-type variable, allocates a space in the stack, stores a reference, and the reference points to a managed heap.

Value types: struct, enum, numeric type, BOOL type
Reference types: Arrays, classes, interfaces, delegates (delegate), object,string

You can look at the following example

public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}

public class Referenceandvalue
{
public static void Main ()
{
Person Zerocool = new Person {Name = ' zerocool ', age = 25};
Person Anders = new Person {Name = ' anders ', age = 47};

int age = Zerocool. Age;
Zerocool. age = 22;

Person guru = Anders;
Anders. Name = "Anders Hejlsberg";

Console.WriteLine ("Zerocool ' s age:\t{0}", Zerocool.                    Age); 22
Console.WriteLine ("Age ' s value:\t{0}", age); 25
Console.WriteLine ("Anders ' Name:\t{0}", Anders.                   Name); Anders Hejlsberg
Console.WriteLine ("Guru ' name:\t{0}", Guru.                         Name); Anders Hejlsberg
Console.readkey ();
}
}

Code Analysis:

We first defined a person class that contains 2 attributes, age and name, where age is a value type, and name is a string reference type

It then initializes 2 person classes, Zerocool and Anders, assigns values, and then modifies the age and name

At this time, due to the characteristics of the value type, we just allocated a resource to zerocool.age on the stack, and there was no relationship between the later declared age. So age is the same.

Modifying the name is not the same, because the reference type is modified. Name and Anders. The name is associated, so it changes as well.

So there will be later output.

Besides, Packing & unpacking

Boxing occurs in value types to reference type conversions

Unboxing occurs in reference types toward value type conversions

To convert a value type to a reference type, a boxing operation is required (boxing):

1. First allocate memory from the managed heap for the newly generated reference object.

2. Then copy the data of the value type to the memory you just allocated.

3. Returns the address of the newly allocated object in the managed heap.

As you can see, the two comparisons that are performed to allocate memory and copy data in a single boxing operation that affect performance.

Converting a reference inner type into a value type requires a unboxing operation (unboxing):

1. First get the address of the part of the managed heap that belongs to the value type, and this step is the strict unboxing.

2. Copy the values from the Reference object to the instance of the value type on the thread stack.

After these 2 steps, it can be thought that the same boxing is the reciprocal operation. The strict unboxing does not affect performance, but the subsequent operation of the copied data will affect performance as well as the boxing operation.

Give me another example that I usually use.

When we are:
for (int i = 0; i < arr. Length; i++)
{
//
}

This is why we are more like this:
int L = arr. Length;
for (int i = 0; i < L; i++)
{
//
}

I <arr. Length, in fact, the boxing, if repeated many times, then there will be memory loss, so you can first boxing, then cycle.

can also be avoided by overloading functions, avoiding boxing by generics (not yet understood)

Like Console.WriteLine () This method is also likely to involve boxing and so on operations

Some relatively good introduction and reference:

Http://www.cnblogs.com/huashanlin/archive/2007/05/16/749359.html

Http://www.cnblogs.com/hunts/archive/2007/01/19/boxing_unboxing.html

Value types & Reference types, boxing & unpacking

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.