KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Since a struct in C# consists of the bits of its members, you cannot have a value type T which includes any T fields: // Struct member 'T.m_field' of type 'T' causes a cycle in the struct layout struct T { T m_field; } My understanding is that an instance of the above type could never be instantiated*—any attempt to do so would result in an infinite loop of instantiation/allocation (which I guess would cause a stack overflow? ** )—or, alternately, another way of looking at it might be that the definition itself just doesn't make sense; perhaps it's a self-defeating entity, sort of like "This statement is false." Curiously, though, if you run this code: BindingFlags privateInstance = BindingFlags.NonPublic | BindingFlags.Instance; // Give me all the private instance fields of the int type. FieldInfo[] int32Fields = typeof(int).GetFields(privateInstance); foreach (FieldInfo field in int32Fields) { Console.WriteLine("{0} ({1})", field.Name, field.FieldType); } ...you will get the following output: m_value (System.Int32) It seems we are being "lied" to here***. Obviously I understand that the primitive types like int , double , etc. must be defined in some special way deep down in the bowels of C# (you cannot define every possible unit within a system in terms of that system... can you?—different topic, regardless!); I'm just interested to know what's going on here . How does the System.Int32 type (for example) actually account for the storage of a 32-bit integer? More generally, how can a value type (as a definition of a kind of value) include a field whose type is itself ? It just seems like turtles all the way down . Black magic? <sub
Tags (comma-separated)
Save Edits
Cancel