C # how to identify the actual type of reference (1) -- reprinted

Source: Internet
Author: User

You can skip this step if you are familiar with it.

1. What is reference?

A reference is a data structure that contains the value of a computer memory heap address, just like a pointer in C ++. In this article, all the words "Reference" appear, readers can understand it as a pointer in C, C ++.

Again, references are different from pointers. For example, GC modifies the reference value when reclaim the memory. However, this article does not focus on the differences between references and pointers, readers can understand all the "references" that appear here as pointers in C, C ++.

2 stack and heap, memory allocation in. Net?

Http://blog.csdn.net/cuike519/archive/2009/12/23/5063333.aspx, if you're not very clear about this, here you have what you need to know

The method is class, and the non-static field is instance

Methods In the class (static or non-static) are essentially class. Non-static fields in the class belong to the class instance.

It is precisely because of this difference that the methods in the classCodeOnly one copy is created in the memory, and the non-static fields in the class are created with the creation of the class instance.

Theoretically, you only need to allocate memory in the memory for an instance class object that is consistent with the total size of all non-static fields in the class.

Then, assign the first address of the allocated heap memory address to the reference in the stack.

However, the actual situation is not so simple. net, when instantiating a class, a total of memory allocated (sizeof (void *) + total of all non-static field sizes in the class)

So what is the extra sizeof (void *) used?

P.s. If I remember correctly, under a 32-bit CPU, the size of sizeof (void *) is 4 bytes.

What is the 4 byte?

The reference points to a memory address. After this address + sizeof (void *), the reference follows the non-static field of the instance.

Each class has a sizeof (void *)-sized "excess" memory. So what exactly is there?

There are two items in total.

The first is the index named syncblock.

The second is a handle pointing to a public data structure.

Do not underestimate the undisclosed data structure, where there are actual data types ,. net relies on it to accurately identify the actual type of reference. Even if you follow the example below, the CLR will know who is who

Subclass sub = new subclass ();

Bassclass B = (bassclass) sub;

B. GetType (); // CLR said, do not install B. I know that you are an instance of the subclass class.

I really want to know more?

1. the undisclosed data structure has a name called corinfo_class_struct (this name is just for reference. In reality, it may not be the name ), we cannot use programming methods to directly access this data structure,. NET provides a class, system. runtimetypehandle allows us to access it using the defined method.

For more information about system. runtimetypehandle, see msdn

2. msil provides two IL commands for inter-type conversion.

Isinst

Castclass

Both commands generate access to corinfo_class_struct to verify whether the conversion can be performed.

The only difference between them is that when verification fails, castclass throws a system. invalidcastexception (familiar with it ?)

Isint will add a null reference to the top of the stack (msil is a stack-based language. If you are interested in it, Google :-))

In C #

As, is operator is converted to isinst

() The forced type conversion character is converted to castclass.

 

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.