Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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.

Ashish Sheth
GOOD ANSWER
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...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Tom Porterfield
GOOD ANSWER
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...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
GOOD ANSWER
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...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Daniel Jin (VIP)
GOOD ANSWER
"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...
Vote that this is a GOOD answer... (5 votes from other users already)
 
 
    
Jon Skeet [C# MVP] (VIP)
GOOD ANSWER
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...
Vote that this is a GOOD answer... (4 votes from other users already)
 
 
    
Mike Schilling
GOOD ANSWER
"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...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Jon Skeet [C# MVP] (VIP)
GOOD ANSWER
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...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Mike Schilling
GOOD ANSWER
"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...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Jon Skeet [C# MVP] (VIP)
GOOD ANSWER
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...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
jonathan middleton
actually, GetType() is a class method inherited by System.Int32.
it is the implicit boxing that makes that i.GetType() meaningful
and also explains the result.
Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Joe Mayo [C# MVP] (VIP)
GOOD ANSWER
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...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Mike Schilling
GOOD ANSWER
"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...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Joe Mayo [C# MVP] (VIP)
GOOD ANSWER
"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...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
 
System.Console
System.Int32
System.Object
System.ValueType




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734