Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation between System.Object class and Structs

Tags:

c#

clr

I know that my question seems dumb but I am confused. I appreciate if someone clarify this for me.

I know that structs, e.g. Int32, are value types and are instantiated on stack while classes are reference types and are instantiated on heap. I also know that all the structs are derived from System.Object type, which is a class. I wonder how this is possible that the supertype, System.Object, is a reference type and the subtype, Int32, is a value type? Where should I look to understand how this works?

like image 316
Morteza Avatar asked Mar 23 '12 07:03

Morteza


People also ask

What is difference between struct and object?

Obviously you can blur the distinctions according to your programming style, but generally a struct is a structured piece of data. An object is a sovereign entity that can perform some sort of task. In most systems, objects have some state and as a result have some structured data behind them.

What is the difference between structs and classes?

The only difference between a struct and class in C++ is the default accessibility of member variables and methods. In a struct they are public; in a class they are private.

Do structs inherit from object?

Yes, all struct s inherit from System. ValueType which in turn inherits from System. Object .

What are the similarities and difference between class and structure?

Structures and classes differ in the following particulars: Structures are value types; classes are reference types. A variable of a structure type contains the structure's data, rather than containing a reference to the data as a class type does. Structures use stack allocation; classes use heap allocation.


1 Answers

I know that structs, e.g. Int32, are value types and are instantiated on stack while classes are reference types and are instantiated on heap.

You do not know that, because in order to be classified as knowledge a belief must be true. That belief is certainly not true, though many people believe it.

Value types are sometimes allocated on the stack. References are sometimes allocated on the stack and refer to memory allocated on the heap. Value types are sometimes allocated on the heap. (And of course value types and references may also be allocated in registers which are neither stack nor heap.)

What determines what storage is allocated on the heap and what is allocated on the stack is the known lifetime requirements of the storage, not what kind of thing it is. If a variable is known to be short-lived then it can be allocated on the stack; if it is not known to be short-lived then it must be allocated on the heap.

I also know that all the structs are derived from System.Object type, which is a class. I wonder how this is possible that the supertype, System.Object, is a reference type and the subtype, Int32, is a value type?

I suspect that you don't understand what "inherits from" means in the first place if this is confusing to you. When we say that System.Int32, a value type, inherits from System.Object, a reference type, we mean that all members of Object are also members of Int32. Object has a method "ToString". That's a member of Object. Therefore Int32 also has a method "ToString", because Int32 inherits from Object.

That is all that inheritance means. I suspect that you believe that inheritance means something else, and that whatever that "something" is precludes value types from extending reference types. Whatever belief you have that makes you think that value types cannot inherit from reference types is clearly false, since obviously they do.

I am interested to learn what false things people believe about programming languages; what is it that you think incorrectly is true about the inheritance relationship that would preclude value types from deriving from reference types?

You should read and understand all of these articles:

http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx

http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

http://ericlippert.com/2011/09/19/inheritance-and-representation/

And extra bonus, this one might also be helpful to you:

http://blogs.msdn.com/b/ericlippert/archive/2012/01/16/what-is-the-defining-characteristic-of-a-local-variable.aspx

like image 191
Eric Lippert Avatar answered Oct 08 '22 02:10

Eric Lippert