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