[C #] How does struct support virtual method overwrite and interface inheritance?

Source: Internet
Author: User

I have not studied monographs such as CLR Via C #, But I have doubts in my mind. Then I will go to my personal inquiry. I will explain more in the following sections. If you want to know the story, tell C # the background the truth.

My own conclusions are as follows:

1 // original declaration

2 struct People: IFormattable

3 {

4 public string ToString (string format, IFormatProvider formatProvider)

5 {

6 return ToString ();

7}

8 public override string ToString () {return Name ;}

9

10 public string Name {get; set ;}

11}

Will be converted into two background declarations:

1 // The struct People in the actual code is mapped to this type. The virtual method declaration and interface inheritance are invalid.

2 struct PeoplePOD //: IFormattable

3 {

4 public string ToString (string format, IFormatProvider formatProvider)

5 {

6 return ToString ();

7}

8 public/* override */string ToString () {return Name ;}

9

10 public string Name {get; set ;}

11}

12 // This is the heap object type after packing. Any transformation that tries to convert the original struct to object/ValueType/interface will be automatically packed into this type of object.

13 class PeopleBox: IFormattable

14 {

15 public string ToString (string format, IFormatProvider formatProvider)

16 {

17 return ToString ();

18}

19 public override string ToString () {return Name ;}

20

21 public string Name {get; set ;}

22}

All the struct People in the Code are actually struct PeoplePOD, that is, the pure data + non-virtual method inherited by virtual functions and interfaces is abandoned. Without the virtual method, the object instance does not need to include a pointer pointing to the type information to support polymorphism. Therefore, the size obtained from sizeof for this struct is equal to that of each field (excluding the class field) the sum of sizeof values.

Any conversion from the original struct object to the base class will cause packing. The packing type is PeopleBox:

1 People orgin = new People ();

2 object _ object = orgin; // object _ object = new PeopleBox (orgin );

3 ValueType _ valueType = orgin; // ValueType _ valueType = new PeopleBox (orgin );

4 IFormattable _ iformattable = orgin; // IFormattable _ iformattable = new PeopleBox (orgin );

Therefore, the virtual functions and interface inheritance declared in struct only fully exert the effect on the object after packing. For struct object itself, these virtual methods are degraded into static calls (binding during compilation ).
Green Channel: Please follow my favorites to contact me

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.