473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct Vs Classes

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
Nov 16 '05 #1
10 5725
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]
- mv*@spam.guard. caspershouse.co m
"Ashish Sheth" <ea*****@gmail. com> wrote in message
news:O9******** ******@TK2MSFTN GP09.phx.gbl...
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

Nov 16 '05 #2
Ashish Sheth wrote:
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.
--
Tom Porterfield
Nov 16 '05 #3
Daniel Jin <Da*******@disc ussions.microso ft.com> wrote:
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).


as an interesting side note, System.ValueTyp e itself is in fact not a value
type.


As another interesting side note, the value types themselves don't
inherit from System.ValueTyp e, 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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" <ea*****@gmail. com> wrote in message
news:O9******** ******@TK2MSFTN GP09.phx.gbl...
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

Nov 16 '05 #5

"Joe Mayo [C# MVP]" <jm***@nospamAt CSharpDashStati on.com> wrote in message
news:ud******** ******@TK2MSFTN GP09.phx.gbl...
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.
Nov 16 '05 #6


"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...

"Joe Mayo [C# MVP]" <jm***@nospamAt CSharpDashStati on.com> wrote in message
news:ud******** ******@TK2MSFTN GP09.phx.gbl...
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.


That is true. Thanks. ;)

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

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel Jin <Da*******@disc ussions.microso ft.com> wrote:
> 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).


as an interesting side note, System.ValueTyp e itself is in fact not a
value
type.


As another interesting side note, the value types themselves don't
inherit from System.ValueTyp e, 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.
Nov 16 '05 #8
Mike Schilling <ms************ *@hotmail.com> wrote:
As another interesting side note, the value types themselves don't
inherit from System.ValueTyp e, 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.


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Mike Schilling <ms************ *@hotmail.com> wrote:
> As another interesting side note, the value types themselves don't
> inherit from System.ValueTyp e, 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.


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.WriteLi ne(i.GetType()) ;
Console.WriteLi ne(i.GetType(). BaseType);
Console.WriteLi ne(i.GetType(). GetHashCode());
Console.WriteLi ne(o.GetType()) ;
Console.WriteLi ne(0.GetType(). BaseType);
Console.WriteLi ne(o.GetType(). GetHashCode());
Console.WriteLi ne(i.GetType() == o.GetType());
}

Its output is:

System.Int32
System.ValueTyp e
113273860
System.Int32
System.ValueTyp e
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:-)
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4658
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
4
31580
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being, a Class can have private and protected members, but a Struct everything is Public by default. I laymans terms what would be an appropriate reason to choose a Struct over a Class? So why would one want to choose a Class over a Struct.
5
7192
by: Chua Wen Ching | last post by:
Hi there, I am very curious on this code: // declared as structure public struct Overlapped { public IntPtr intrnal; public IntPtr internalHigh;
5
8735
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
11
2241
by: dimension | last post by:
If my intentions are to create objects that encapsulates data rows in a table, is it better to use a Struct or Class? Based on what i read, if my objects will simply have get and set methods, Struct is may be better...but i am looking for some advise from the experts on this? Assumptions: it is possible for some operations, i may have 8000 to 10000 (or more) objects instantiated. what are some considerations in designing a system that...
16
16432
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
5
4125
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
46
2520
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
5
2963
by: dis_is_eagle | last post by:
Hi.I would like to know the difference between a C struct and a C++ struct.I think in a C++ struct member variables and functions are by default private whether in a C struct they are public by default.Is there any other differences? Does a C++ struct support inheritence like a C++ class?Thanks for any help. Regards, Eric
74
16041
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.