struct

T

Tony Johansson

Hi!

There is no inheritance for structs as there is for classes. A struct cannot
inherit from another struct or class, and it cannot be the base of a class
but despite this struct can inherit from the root class Object.

How is that possible ?

//Tony
 
P

Peter Duniho

Tony said:
Hi!

There is no inheritance for structs as there is for classes. A struct cannot
inherit from another struct or class, and it cannot be the base of a class
but despite this struct can inherit from the root class Object.

How is that possible ?

Because your initial assumption is wrong. It's not true that there is
no inheritance for structs. What is true is that there is no
_used-defined_ inheritance for structs.

Note that not only do all structs inherit System.Object (all types do),
all structs also inherit System.ValueType, and enums inherit both
System.ValueType and System.Enum. The may be other value type
inheritances I'm forgetting at the moment.

Pete
 
G

Göran Andersson

Tony said:
Hi!

There is no inheritance for structs as there is for classes. A struct cannot
inherit from another struct or class, and it cannot be the base of a class
but despite this struct can inherit from the root class Object.

How is that possible ?

//Tony

A structure inherits from ValueType, which in turn inherits from Object.

The ValueType class is treated specially by the compiler, so a value
type isn't stored the way that objects are, with a type identifier and
virtual method table reference prepended to them. A value type is stored
as just it's actual data, so an Int32 for example is stored only as the
four bytes of data that represents the integer value.

So, the ValueType class is kind of a barrier between the Object form and
the simple data types, which allows the language to treat the simple
data types in an object oriented fashion, while they are still handled
in an efficient way without the bloat of objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top