Struct Vs Classes

A

Ashish Sheth

Hi Gurus,
In C# why struct can't be inherited from another struct, just like a class
can be inherited from another class? another question is all struct by
default are inherited from System.Object class, how this is possible when it
is not allowed to derive a struct from a class? and System.Object is the
reference type provided by the .NET Framework then why structs are value
types? since the struct are derived from System.Object then they should be
also a reference type. Can anybody answer this questions?

thanks and regards,
Ashish Sheth
 
N

Nicholas Paldino [.NET/C# MVP]

Ashish,

A structure is a special case type in .NET, and is considered to have
certain semantics. Also, structures are derived from ValueType, not Object
(but ValueType is derived from Object).

Hope this helps.
 
T

Tom Porterfield

Ashish said:
Hi Gurus,
In C# why struct can't be inherited from another struct, just like a class
can be inherited from another class? another question is all struct by
default are inherited from System.Object class, how this is possible when it
is not allowed to derive a struct from a class? and System.Object is the
reference type provided by the .NET Framework then why structs are value
types? since the struct are derived from System.Object then they should be
also a reference type. Can anybody answer this questions?

Your logic is flawed. Numeric types such as int and decimal also derive
from System.Object, so following your logic an int should be a reference
type and should be allowed to derive from a class or be derived from.

As structs are value types, unlike classes they do not require heap
allocation. Also, as value types they cannot be derived from. Structs
can implement an interface.
 
J

Jon Skeet [C# MVP]

Daniel Jin said:
as an interesting side note, System.ValueType itself is in fact not a value
type.

As another interesting side note, the value types themselves don't
inherit from System.ValueType, either. Instead, their corresponding
boxed types inherit from ValueType, and they don't inherit from
anything.

It's a murky world out there...
 
J

Joe Mayo [C# MVP]

Hi Ashish,

In addition to what the other people said, there is a concept called Type
System Unification, which allows everything to be treated as an object. It
kind of gives you the best of all worlds with the benefits of performance
with struct/value types and the power of object-oriented programming with
reference types. I discuss it more in the following article:

http://www.csharp-station.com/Articles/ObjectClass.aspx

BTW, comments on this article are welcome.

Joe
 
M

Mike Schilling

Joe Mayo said:
Hi Ashish,

In addition to what the other people said, there is a concept called Type
System Unification, which allows everything to be treated as an object.
It
kind of gives you the best of all worlds

(cough, cough, cough)

That is, it makes different tradeoffs than do systems that distinguish
objects from scalar types.
 
J

Joe Mayo [C# MVP]

Mike Schilling said:
(cough, cough, cough)

That is, it makes different tradeoffs than do systems that distinguish
objects from scalar types.

That is true. Thanks. ;)

Joe
 
M

Mike Schilling

Jon Skeet said:
As another interesting side note, the value types themselves don't
inherit from System.ValueType, either. Instead, their corresponding
boxed types inherit from ValueType, and they don't inherit from
anything.

Can you enlarge on that? Consider:

int i = 12;
Object o = i;
int j = (int)o;

While it's clear that boxing and unboxing are taking place, it's not
obviuous (to me, now) that type conversion is taking place as well.
 
J

Jon Skeet [C# MVP]

Mike Schilling said:
Can you enlarge on that? Consider:

int i = 12;
Object o = i;
int j = (int)o;

While it's clear that boxing and unboxing are taking place, it's not
obviuous (to me, now) that type conversion is taking place as well.

It is, because the boxed type isn't the same as the value type. Boxing
and unboxing themselves are conversions.

This is hidden at any number of levels from the developer, to be
honest, but if you look at the CLI spec it's all there. Just don't
expect to come out again with your mind in tact :)
 
M

Mike Schilling

Jon Skeet said:
It is, because the boxed type isn't the same as the value type. Boxing
and unboxing themselves are conversions.

This is hidden at any number of levels from the developer, to be
honest, but if you look at the CLI spec it's all there. Just don't
expect to come out again with your mind in tact :)

Here's another program.

public static void Main()
{
int i = 12;
Object o = (Object)i;

Console.WriteLine(i.GetType());
Console.WriteLine(i.GetType().BaseType);
Console.WriteLine(i.GetType().GetHashCode());
Console.WriteLine(o.GetType());
Console.WriteLine(0.GetType().BaseType);
Console.WriteLine(o.GetType().GetHashCode());
Console.WriteLine(i.GetType() == o.GetType());
}

Its output is:

System.Int32
System.ValueType
113273860
System.Int32
System.ValueType
113273860
True

This doesn't disprove what you said, of course, it shows only that GetType()
on an unboxed object returns the boxed type, or conversely that GetType() on
a boxed object returns the unboxed type, which thereupon lies about its
ancestry. Once dishonesty enters the type system, it's difficult to know
who to believe:)
 
J

Jon Skeet [C# MVP]

Mike Schilling said:
Here's another program.

public static void Main()
{
int i = 12;
Object o = (Object)i;

Console.WriteLine(i.GetType());
Console.WriteLine(i.GetType().BaseType);
Console.WriteLine(i.GetType().GetHashCode());
Console.WriteLine(o.GetType());
Console.WriteLine(0.GetType().BaseType);
Console.WriteLine(o.GetType().GetHashCode());
Console.WriteLine(i.GetType() == o.GetType());
}

Its output is:

System.Int32
System.ValueType
113273860
System.Int32
System.ValueType
113273860
True

This doesn't disprove what you said, of course, it shows only that GetType()
on an unboxed object returns the boxed type, or conversely that GetType() on
a boxed object returns the unboxed type, which thereupon lies about its
ancestry. Once dishonesty enters the type system, it's difficult to know
who to believe:)

Well exactly. Most people don't really *need* to know that the the type
system has two different representations, etc - GetType() and the like
smudge the issue, but in a way which is arguably more useful than a
precise but hard-to-work-with set of methods. The spec, however, needs
to be a lot more precise, and is.
 

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

Similar Threads

struct 2
Why are some types implemented as struct? 7
?? 'new' and 'protected' Modifiers on Structs ?? 13
Interface vs Struct 7
struct or class 9
Using Delegates? 2
Struct faster than class? 17
CCW COM and Parameters 1

Top