Struct Vs Classes
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

Reply to this message...
 
    
Tom Porterfield
Ashish Sheth wrote:
[Original message clipped]

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.
--
Tom Porterfield
Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
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.

--
- Nicholas Paldino [.NET/C# MVP]
- Click here to reveal e-mail address

"Ashish Sheth" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Daniel Jin (VIP)
"Nicholas Paldino [.NET/C# MVP]" wrote:

[Original message clipped]

as an interesting side note, System.ValueType itself is in fact not a value
type.
Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Daniel Jin <Click here to reveal e-mail address> wrote:
[Original message clipped]

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...

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Mike Schilling
"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

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.

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Mike Schilling <Click here to reveal e-mail address> wrote:
[Original message clipped]

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 :)

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Mike Schilling
"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

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:-)

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Mike Schilling <Click here to reveal e-mail address> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Joe Mayo [C# MVP] (VIP)
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
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com

"Ashish Sheth" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Mike Schilling
"Joe Mayo [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

(cough, cough, cough)

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

Reply to this message...
 
    
Joe Mayo [C# MVP] (VIP)
"Mike Schilling" <Click here to reveal e-mail address> wrote in message
news:%23%Click here to reveal e-mail address...
[Original message clipped]

That is true. Thanks. ;)

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com

Reply to this message...
 
 
System.Console
System.Int32
System.Object
System.ValueType




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com