On the value types and reference types in C # _c# tutorial

Source: Internet
Author: User

I. Basic CONCEPTS

C # has only two types of data: value types and reference types

Value type thread stack allocation space, reference type in the managed heap allocation space

A value type is converted to a reference type, which is called a boxing, and a reference type to a value type known as a unboxing

The following is a comparison of value types and reference types

As you can see from the diagram above: String,object, Array, class is reference type, simple type, enumeration, struct is value type.

Second, code display

Define a class and struct call assignment

The memory allocation scenario is as follows:

From this diagram, we can see that the class instantiation object points to the allocated space in the memory heap; the object that truct instantiated is allocated in the memory stack.

Modify the code as follows:

Memory allocation:

As the above illustration can tell:

Copy Code code as follows:

Object obj= "ABC";
String i= (string) obj;

Value types and reference types are stored in a different location
If it's a reference type, when two objects are pointing to the same place, modifying one, the value of the other object will change

Using System;

Namespace ConsoleApplication2
{
  //reference type (because ' class ') Public
  class Someref
  {public
    int × {get; set;}
  }
  Value type (because ' struct ') public
  struct Someval
  {public
    int x{get; set;}
  }

  Class program
  {
    static void Main (string[] args)
    {
      someref r1=new someref ()////on heap
      someval V1 = new Someval (),//allocated on the stack
      r1.x = 5;    The collar pointer
      v1.x = 5;    Modify Console.WriteLine on the Stack
      (r1.x);//Show 5 
      Console.WriteLine (v1.x);//Show 5

      someref R2 =r1;//copy references only (pointers)
      someval v2 =V1;///Allocate and assign member
      r1.x = 8 on the stack;    r1.x and r2.x will modify
      v1.x = 8;    v1.x will change, v2.x will not

      Console.WriteLine (r1.x);//Show 8 
      Console.WriteLine (r2.x);//Show 8 
      Console.WriteLine ( v1.x); Display 8
      Console.WriteLine (v2.x);//Show 5

      console.readkey ();
    }
  }
}

Three, value type box, unboxing

1, Boxing: is the value of the type of data packaging into the reference type of the instance (for example, the string of value ABC assigned to object obj)

Copy Code code as follows:
String i= "ABC";
Object Obj= (object) I;

What exactly happens when boxing is done:

1), allocating memory in the managed heap. The amount of memory allocated is the amount required for each field of a value type, plus the amount required for the two additional members (type Object pointers and synchronized block indexes) of all objects in the managed heap

2), field of value type copied to the newly allocated heap memory

3), return the object address, now the address is an object reference; value type is a reference type

2, unboxing: is to extract the value type from the reference data (for example, the value of object obj is assigned to the variable I of type string)

Copy Code code as follows:
Object obj= "ABC";
String i= (string) obj;

Unpacking is not a boxing process upside down, the cost of unpacking is much lower than the box, the unboxing is the process of getting the pointer to the original value type contained in an object.

The value of the field contained in the heap is copied to the value type instance of the stack after the box is removed.

3. A value type becomes a reference type and does not necessarily have to be boxed, for example:

Copy Code code as follows:
String str = "Joye.net" + 26; You need to boxing 26 to a string type
String str1 = "Joye.net" + 26.ToString (); No boxing required after ToString

Interested can take a look at IL.

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.