Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Structures vs. Classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thomasfarrow@gmail.com - 10 Mar 2006 09:04 GMT
At work, our development team has a development standards document that

insists Structures should never be used. I'm looking to change this
standard but need a suitable argument in order to make the change. I
know that Structures are value types, sit on the stack, and are
generally more efficient to manipulate than reference types (i.e.
Classes). Structures cannot use inheritance, the finalize method or
default constructors. Can anyone think of a situation where using
Structures is more suitable than using Classes?
Frans Bouma [C# MVP] - 10 Mar 2006 09:33 GMT
> At work, our development team has a development standards document
> that
[quoted text clipped - 6 lines]
> default constructors. Can anyone think of a situation where using
> Structures is more suitable than using Classes?

    Structures should be used ONLY for very small value types, so if the
struct refers to an object, it's likely not a valid candidate to be a
struct.

    Structs have the disadvantage that if you store them in an arraylist
or List<T>, manipulating them isn't as convenient as you'd think:

myList[index].MyProperty = value;
won't work, as you're manipulating the value you get from the indexer,
not the value INSIDE the list. This can lead to bugs you only encouter
at runtime, and therefore most people opt for classes instead, which is
in general the right thing to do.

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Michael D. Ober - 10 Mar 2006 13:47 GMT

The only time I use structures is when two or more related simple data types
(int, boolean, string, etc.) need to be grouped together.  Even in this
case, I tend to use classes because I can easily change a public property
into a private property with a public property SET/GET block of code that
cleans up the data.  Also, structures don't have a constructor to allow for
default value setting.

Mike Ober.

> At work, our development team has a development standards document that
>
[quoted text clipped - 5 lines]
> default constructors. Can anyone think of a situation where using
> Structures is more suitable than using Classes?
Kevin Spencer - 10 Mar 2006 14:15 GMT
> Also, structures don't have a constructor to allow for
> default value setting.

Actually, they do. See
http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_11_3_8.asp?f
rame=true


Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

> The only time I use structures is when two or more related simple data
> types
[quoted text clipped - 16 lines]
>> default constructors. Can anyone think of a situation where using
>> Structures is more suitable than using Classes?
Michael D. Ober - 10 Mar 2006 16:52 GMT

Kevin - my error.

OP - Here's a link to the VB 2005 documentation that highlights the
similarities and differences between classes and structures.

http://msdn2.microsoft.com/en-us/library/2hkbth2a(VS.80).aspx

Mike.

>  > Also, structures don't have a constructor to allow for
> > default value setting.
>
> Actually, they do. See

http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_11_3_8.asp?f
rame=true


> > The only time I use structures is when two or more related simple data
> > types
[quoted text clipped - 16 lines]
> >> default constructors. Can anyone think of a situation where using
> >> Structures is more suitable than using Classes?
Cowboy (Gregory A. Beamer) - MVP - 10 Mar 2006 17:56 GMT
Pros:
1. Greater perf as it takes less time to allocate memory on the stack that
the heap - this is miniscule, so don't use this as a reason, unless other
perf tweaks have failed and you are at the end of your rope (even then, I
might hang myself before switching to a struct system)
2. More deterministic clean up. The stack is guaranteed to die when the
appDomain is unloaded, while the heap, technically, can live on - this is a
minor benefit in most cases, as you are not in control of memory either way

Cons
1. Cannot code the default constructor. If you need to spin up an object
with some instantiation work, you have to have at least one parameter. This
has to do with a default construct already hard wired.
2. When you use interfaces, the struct is boxed and you have to explicitly
unbox it if you desire to use it as a value type. This means you lose one of
the benefits of structs.

Implications (neither pro or con):
1. Structs are passed, as parameters, by value. This means a new “object” is
created internally. Classes are passed by reference. Depending on your
system, this can be a big deal.

Overall, structs are good for small "collections" of value types
(implemented as properties or fields), when you do not need to add methods or
adhere to an interface, like a User Defined Type (UDF), and when passing by
reference does not make sense.

Signature

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

> At work, our development team has a development standards document that
>
[quoted text clipped - 5 lines]
> default constructors. Can anyone think of a situation where using
> Structures is more suitable than using Classes?
Jon Skeet [C# MVP] - 10 Mar 2006 19:33 GMT
Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
wrote:

<snip>

> Implications (neither pro or con):
> 1. Structs are passed, as parameters, by value. This means a new ?object? is
> created internally. Classes are passed by reference. Depending on your
> system, this can be a big deal.

Just for future reference - classes (objects) are *not* passed by
references by default. A *reference* is passed *by value*. There's a
big difference.

See http://www.pobox.com/~skeet/csharp/parameters.html

(I'm picky about this because I've seen it confuse people who genuinely
understand the difference between pass-by-reference and pass-by-value
semantics. They hear that "objects are passed by reference" and expect
things to work which don't.)

Signature

Jon Skeet - <skeet@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

dkode - 10 Mar 2006 19:37 GMT
Like someone else mentioned,

I only use structures when I have many (> 1000 instances) of the same
struct to create, and even then only when there is only one or two
values that I need to place into the struct (like two strings, or an
int and a string)

I rarely use structs, but sometimes I opt for them if I don't need a
constructor or any other fancy manipulation of the structs.
Kevin Spencer - 11 Mar 2006 14:10 GMT
Always keeping us honest, Jon! While this does seem picky, I have
(reluctantly) benefitted from your "nagging" in that it has forced me to be
more accurate, and to study and understand things on a deeper level. For
example, I have made a thorough study of .Net memory management, and am
highly unlikely to confuse what goes on the stack and what goes on the heap
ever again! From time to time, this understanding has proven invaluable to
me personally.

;-)

Signature

Kevin Spencer
Microsoft MVP
.Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

> Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
> wrote:
[quoted text clipped - 17 lines]
> semantics. They hear that "objects are passed by reference" and expect
> things to work which don't.)
Jon Skeet [C# MVP] - 10 Mar 2006 19:34 GMT
> At work, our development team has a development standards document that
> insists Structures should never be used. I'm looking to change this
> standard but need a suitable argument in order to make the change. I
> know that Structures are value types, sit on the stack

Small point - structures don't always sit on the stack. They sit
wherever the variable lives (for variables). See
http://www.pobox.com/~skeet/csharp/memory.html

I would suggest that you don't bother trying to change the standard
until you've got a really good candidate for a value type. They do
exist, but they're few and far between in my experience.

Signature

Jon Skeet - <skeet@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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.