473,378 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Struct verses class.

Hi all,

Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.

Because if i have code like
class a
{

}
struct a1
{
}

As above declaration i have declared class as well as struct with no
paramter. Still it is acceptable.

So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.

thanks in advance.

Jun 1 '06 #1
7 3432
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.
That's completely incorrect.

<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.


You're doing nothing wrong. Where did you hear the above statement? I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)

Jon

Jun 1 '06 #2

Jon Skeet [C# MVP] wrote:
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.
That's completely incorrect.


But true of the almost-equivalent things in VB.NET.
<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.
You're doing nothing wrong. Where did you hear the above statement?


<http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconStructuresAndClasses.asp>,
or similar, possibly.
I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)


Context is everything, isn't it :)

--
Larry Lard
Replies to group please

Jun 1 '06 #3
Hi Jon,

Thanks for your reply.

Larry has providied link of document which contains this statement.

Thats why i was asking question.

thanks

Jon Skeet [C# MVP] wrote:
archana wrote:
Can someone tell me meaning of this stmt:-

'A structure must have at least one nonshared variable or event member;
a class can be completely empty'.


That's completely incorrect.

<snip>
So can some one tell me what am i doing wrong and please clear me
reagrding above menion statement.


You're doing nothing wrong. Where did you hear the above statement? I'd
start treating any other statements from the same source with a pinch
of salt. (Of course, it's possible that that is the only incorrect
statement they've made - but it's worth checking.)

Jon


Jun 2 '06 #4
archana <tr**************@yahoo.com> wrote:
Thanks for your reply.

Larry has providied link of document which contains this statement.

Thats why i was asking question.


Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 2 '06 #5
>Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.


Not much of a restriction in practice, is it? What good is an empty
struct anyway? If you only have static members you may as well us a
static class.

The CLR requires value types to either have instance fields or an
explicit size so for empty structs the C# compiler must effectively
add [StructLayout(..., Size=1)]. So now you have a type that appears
to be empty but still actually takes up one byte. I'm not sure I like
that better than what the VB compiler is doing.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 2 '06 #6
Mattias Sjögren wrote:
Right. I've no idea why VB.NET has that restriction, but C# certainly
doesn't.


Not much of a restriction in practice, is it? What good is an empty
struct anyway? If you only have static members you may as well us a
static class.

The CLR requires value types to either have instance fields or an
explicit size so for empty structs the C# compiler must effectively
add [StructLayout(..., Size=1)]. So now you have a type that appears
to be empty but still actually takes up one byte. I'm not sure I like
that better than what the VB compiler is doing.
Mattias


Truly not a restriction in practice.

I can not come up with a single use for an empty structure. A value type
that doesn't carry it's own value is totally useless.

The closest thing to something useful I can think of is creating
different empty structures that would override the ToString method and
return different values. Then you could put instances of the different
structs in something like an ArrayList, and then you could use the
ToString method on the objects in the ArrayList without needing to know
what structure it really was, and they would return different values.
But then again you could just put the strings in the ArrayList and it
would have exactly the same effect...
Jun 3 '06 #7
"Göran Andersson" <gu***@guffa.com> wrote in message
news:O4**************@TK2MSFTNGP03.phx.gbl...
I can not come up with a single use for an empty structure. A value type
that doesn't carry it's own value is totally useless.


Yes. An empty class can be useful, as a placeholder for derived behavior you
know you'll eventually add, but that doesn't apply to structs.

///ark
Jun 3 '06 #8

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

Similar topics

21
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
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
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,...
13
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment. Thanks
2
by: Tom Jones | last post by:
I have a class that contains a collection of reference types. This class needs to have a method that returns the collection to the caller. The method's signature could be either (it could also...
3
by: Tom Jones | last post by:
I do not understand what is meant when someone states that a given method is "hidden" verses overriden. Would someone please provide a short example of both cases and why you might want to...
11
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,...
3
by: ak | last post by:
Q1: I am looking to find/build a table of PHP version verses timezonedb version shipped with it. For example, I think that 5.2.0 updated the timezonedb to the 2006.14 version. Is there a table of...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.