. NET Type (Types),. nettypes

Source: Internet
Author: User

. NET Type (Types),. nettypes
Introduction

Are you a. Net engineer? So, do you know three types in NetFramework? (Besides the reference type and value type, is there ?)

The reference type must be on the "Heap", and the value type must be on the "stack?

What do you know about the layout details of the reference type in the memory?

 

Types category in. Net Framework

 

Besides objectAndString(RespectivelySystem. Object andSystem. String alias), Other shadow values are simple value types.

The following is an excerpt from C # Language Specification 5.0-> 4. Type (page: 77)

   

C # language types are classified into two categories: Value type and reference type ).

The third type is pointer, which can only be used in insecure code.

How do I allocate the reference type and value type in the memory?

This part will be explained through a short piece of code. Let's review it before explaining it.

Differences in memory processing during the assignment of reference type and value type:
  • When a Value Type a (defined as int a = 80;) is assigned to another value type B (int B;), that is (B =, copy the value of a to a, for example;
   

Form myForm = new Form();Size s = new Size (100, 100); // struct = value typeFont f = new Font (“Arial”,10); // class = reference typemyForm.Size = s;myForm.Font = f;

  

Note that in the Code, the Size in myForm. Size and the Font of myForm. Font are Form-type attributes (Property), not types (class type, representing a type ).

In. NetFramework, such usage is extremely common. Do not confuse the two for beginners.

I would like to give you some further knowledge before explaining the Code:
  • Size is of the Struct type, of course, it is of the value type (ValueType)
  • Font is a Class type, of course, it is a reference type (ReferenceType)

 

In the memory of the above Code ,:

S is assigned to the Stack, while f and Form of the Front are allocated to the Stack.

  • In addition, the Font attribute of myForm references the Font type f.
  • The Size attribute of myForm has its own values (Width and Height). It is a copy of Size type s.
  • Here we can see the difference between the value type and the reference type in the value assignment process.

      

    We can modify the Font style of myForm by modifying the value of Font type f, but cannot modify the Size of myFrom by modifying s.

    Basic Structure of reference Object memory Layout

    ObjectHeader (the Thread in its AppDomain locks this object by calling Monitor. Enter)

  • Next, the Method Table pointer (which points to the declared (defined) managed type in AppDomain). If the Assembly is loaded into AppDomain neutral, the Method Table pointer of this type of instance in all AppDomains is the same. The basic building block of the CLR type system is visible in the hosted code. (TypeHandle. Value is an IntPtr)
  • The last part is the value of this type of instance.
  • The address of this instance object of CLR object may also change during garbage collection. (For details, refer to the GC compression process)

     

    \ Sscli20 \ clr \ src \ vm \ object. h

    //// The generational GC requires that every object be at least 12 bytes// in size.   #define MIN_OBJECT_SIZE     (2*sizeof(BYTE*) + sizeof(ObjHeader))

    A. NET object has basically this layout:

    class Object{  protected:    MethodTable*    m_pMethTab;};
    class ObjHeader{  private:    // !!! Notice: m_SyncBlockValue *MUST* be the last field in ObjHeader.    DWORD  m_SyncBlockValue;      // the Index and the Bits};
    Platform Minimum instance size (bytes)
    X86 12 bytes= 2*4 + 4
    X64 24 bytes= 2*8 + 8
    Representative structure of reference Object memory Layout

     

    Common Object

     

    Int a = 1; object B =;

    We all know that this code will be packed. what changes will the original value type change after packing? Take a look at the packing and unpacking steps:

      

    Packing:
    Assign an object instance to the heap and copy the value to the new object. Step by step.
    Step 1: allocate new managed heap memory (size: Value Type instance size plus a method table pointer and a SyncBlockIndex ).
    Step 2: copy the instance field of the value type to the newly allocated memory.
    Step 3: return the address of the newly allocated object in the managed heap. This address is a reference to the object.
    Someone understands this: If Int32 is packed, the returned address points to an Int32. I don't think it can be understood in this way, but this is indeed a problem. It is incomplete, and Int32 does not speak about its essence (in the managed heap ).
    Unpack:
    Check the object instance to make sure it is a boxed value of the given value type. Copy the value from the instance to the value type variable.
    In the book, unpacking only gets the pointer to the value type in the referenced object, and copying the content is triggered by the value assignment statement. I think it doesn't matter. The most important thing is to check the nature of the object instance, and The binning and packing types must match. On this point, on the IL layer, I cannot see the principle. I guess, maybe a method like GetType is called to retrieve the type for matching (because strict matching is required ).

    So I will give you a custom struct. Do you know what will be packed?

    Refer to preventing packing from being implemented to the end. It is also a failure to do only half of it.

    For the convenience of viewing the source code, a source code index table is provided here. SSCLI file index
    Item SSCLI Path
    AppDomain \ Sscli \ clr \ src \ vm \ appdomain. hpp
    AppDomainStringLiteralMap \ Sscli \ clr \ src \ vm \ stringliteralmap. h
    BaseDomain \ Sscli \ clr \ src \ vm \ appdomain. hpp
    ClassLoader \ Sscli \ clr \ src \ vm \ clsload. hpp
    EEClass \ Sscli \ clr \ src \ vm \ class. h
    FieldDescs \ Sscli \ clr \ src \ vm \ field. h
    GCHeap \ Sscli \ clr \ src \ vm \ gc. h
    GlobalStringLiteralMap \ Sscli \ clr \ src \ vm \ stringliteralmap. h
    HandleTable \ Sscli \ clr \ src \ vm \ handletable. h
    InterfaceVTableMapMgr \ Sscli \ clr \ src \ vm \ appdomain. hpp
    Large Object Heap \ Sscli \ clr \ src \ vm \ gc. h
    LayoutKind \ Sscli \ clr \ src \ bcl \ system \ runtime \ interopservices \ layoutkind. cs
    LoaderHeaps \ Sscli \ clr \ src \ inc \ utilcode. h
    MethodDescs \ Sscli \ clr \ src \ vm \ method. hpp
    MethodTables \ Sscli \ clr \ src \ vm \ class. h
    OBJECTREF \ Sscli \ clr \ src \ vm \ typehandle. h
    SecurityContext \ Sscli \ clr \ src \ vm \ security. h
    SecurityDescriptor \ Sscli \ clr \ src \ vm \ security. h
    SharedDomain \ Sscli \ clr \ src \ vm \ appdomain. hpp
    StructLayoutAttribute \ Sscli \ clr \ src \ bcl \ system \ runtime \ interopservices \ attributes. cs
    SyncTableEntry \ Sscli \ clr \ src \ vm \ syncblk. h
    System namespace \ Sscli \ clr \ src \ bcl \ system
    SystemDomain \ Sscli \ clr \ src \ vm \ appdomain. hpp
    TypeHandle \ Sscli \ clr \ src \ vm \ typehandle. h

    More source code reference http://www.projky.com/dotnet

    Reference

     

    The Truth About. NET Objects And Sharing Them Between AppDomains

    Six important. NET concepts: Stack, heap, value types, reference types, boxing, and unboxing

    Shared Source Common Language Infrastructure

    [Translate classic articles] go deep into the. NET Framework to see how the CLR creates runtime objects

    Memory layout of. NET objects

    Managed heap and garbage collection

    C # packing and unpacking [sorting]

    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.