HighTechTalks DotNet Forums  

When to use class and when structure

Dotnet Framework microsoft.public.dotnet.framework


Discuss When to use class and when structure in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Bhuwan Bhaskar
 
Posts: n/a

Default When to use class and when structure - 10-23-2007 , 11:23 PM






Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan



Reply With Quote
  #2  
Old   
Peter Duniho
 
Posts: n/a

Default Re: When to use class and when structure - 10-24-2007 , 12:09 AM






Bhuwan Bhaskar wrote:
Quote:
Hi,

When to use class and when structure in our application?
I think it would be hard, if not impossible, to come up with a good,
100% reliable way to describe when to use one or the other. However,
generally speaking:

A struct is a good choice for relatively small data types, especially if
they will always be contained in something else or kept as a local
variable and where the data type is more about storing data than doing
something. As value types, they can have lower overhead than a class in
certain situations and are often better-suited to immutable types (in
fact, one could argue that it's a bad idea to make a mutable struct).

A class is a good choice for more complex data types, especially for
those that represent some sort of abstract type that has a behavior as
opposed to simply storing values. They are required if you want to take
advantage of OOP techniques based on inheritance. Since classes are a
reference type, you'll want to use a class when you will benefit from
being able to have multiple references to the same instance of data or
otherwise want to use a reference to access the data (passing as a
parameter, for example).

For what it's worth, I almost never create a struct. I almost
always want the flexibility and inheritance features of a class. It
does depend on the exact situation though.

Pete


Reply With Quote
  #3  
Old   
Bhuwan Bhaskar
 
Posts: n/a

Default Re: When to use class and when structure - 10-24-2007 , 04:25 AM



Thanks all
Bhuwan

"Bhuwan Bhaskar" <kxxx (AT) gmail (DOT) com> wrote

Quote:
Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan




Reply With Quote
  #4  
Old   
Andrew Faust
 
Posts: n/a

Default Re: When to use class and when structure - 10-24-2007 , 11:51 PM



In addition to what the other responders said. The most common place I use
struct is if I have a function that needs to return more than 1 value. For
example, suppose you had a function that did integer division. You may want
to return a struct that contained both the quotiant and remainder.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


"Bhuwan Bhaskar" <kxxx (AT) gmail (DOT) com> wrote

Quote:
Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan



Reply With Quote
  #5  
Old   
Kevin Spencer
 
Posts: n/a

Default Re: When to use class and when structure - 10-25-2007 , 06:39 AM



Another consideration is that a class is a reference type while a structure
is a value type. So, if you want to create copies of instances instead of
references when assigning, structures provide a simple way to ensure that
this is done. That is, if you want assignments to default to copies or
values rather than references, use a structure. This is one way to prevent
accidental modification of the data in the original entity. For example:

someClass a = new someClass();
someStruct b = new someStruct();

someClass c;
someStruct d;

a.foo = 5;
c = a;
c.foo = 10; // a.foo also equals 10 now, as c is a reference to a

b.bar = 5;
d = b;
d.bar = 10; // b.bar is still equal to 5

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Bhuwan Bhaskar" <kxxx (AT) gmail (DOT) com> wrote

Quote:
Thanks all
Bhuwan

"Bhuwan Bhaskar" <kxxx (AT) gmail (DOT) com> wrote in message
news:eyO5O1eFIHA.4752 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan






Reply With Quote
  #6  
Old   
Peter Duniho
 
Posts: n/a

Default Re: When to use class and when structure - 10-25-2007 , 02:16 PM



Kevin Spencer wrote:
Quote:
Another consideration is that a class is a reference type while a structure
is a value type. So, if you want to create copies of instances instead of
references when assigning, structures provide a simple way to ensure that
this is done. That is, if you want assignments to default to copies or
values rather than references, use a structure. This is one way to prevent
accidental modification of the data in the original entity.
Just be aware that:

1) This is a shallow copy. If the struct itself contains reference
types, only the references are copied. And,

2) It's not hard to implement the same behavior in a class. If a
shallow copy is sufficient, then it's trivial to add a Clone() method
(implementing IClonable) that calls Object.MemberwiseClone() to do the same.

Personally, I wouldn't call the copying behavior of a struct a
significant difference between the two, given the above. But if it's a
difference that's of interest, one should at least be aware of the
subtleties related to that difference.

Pete


Reply With Quote
  #7  
Old   
Kevin Spencer
 
Posts: n/a

Default Re: When to use class and when structure - 10-26-2007 , 07:11 AM



Good points Peter. Especially the first. I mentioned the value/reference
aspect because there are definitely times when composite types of value
types definitely benefit from being structs rather than classes. But I did
neglect to mention the "gotcha" of structs containing reference types.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Peter Duniho" <NpOeStPeAdM (AT) NnOwSlPiAnMk (DOT) com> wrote

Quote:
Kevin Spencer wrote:
Another consideration is that a class is a reference type while a
structure is a value type. So, if you want to create copies of instances
instead of references when assigning, structures provide a simple way to
ensure that this is done. That is, if you want assignments to default to
copies or values rather than references, use a structure. This is one way
to prevent accidental modification of the data in the original entity.

Just be aware that:

1) This is a shallow copy. If the struct itself contains reference
types, only the references are copied. And,

2) It's not hard to implement the same behavior in a class. If a
shallow copy is sufficient, then it's trivial to add a Clone() method
(implementing IClonable) that calls Object.MemberwiseClone() to do the
same.

Personally, I wouldn't call the copying behavior of a struct a significant
difference between the two, given the above. But if it's a difference
that's of interest, one should at least be aware of the subtleties related
to that difference.

Pete



Reply With Quote
  #8  
Old   
Bhuwan Bhaskar
 
Posts: n/a

Default Re: When to use class and when structure - 10-27-2007 , 02:08 AM



Quote:
struct is if I have a function that needs to return more than 1 value. For
Kindly explain how a function can return more than one value?

Thanks,

Bhuwan

"Andrew Faust" <andrew (AT) andrewfaust (DOT) com> wrote

Quote:
In addition to what the other responders said. The most common place I use
struct is if I have a function that needs to return more than 1 value. For
example, suppose you had a function that did integer division. You may
want to return a struct that contained both the quotiant and remainder.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


"Bhuwan Bhaskar" <kxxx (AT) gmail (DOT) com> wrote in message
news:eyO5O1eFIHA.4752 (AT) TK2MSFTNGP04 (DOT) phx.gbl...
Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan





Reply With Quote
  #9  
Old   
Peter Duniho
 
Posts: n/a

Default Re: When to use class and when structure - 10-27-2007 , 03:36 AM



In article <u7j5DAGGIHA.4684 (AT) TK2MSFTNGP06 (DOT) phx.gbl> "Bhuwan
Bhaskar"<kxxx (AT) gmail (DOT) com> wrote:
Quote:
struct is if I have a function that needs to return more than 1
value. For

Kindly explain how a function can return more than one value?
By putting the values into a struct, and then returning the struct.

As far as the compiler knows, the function is return a single value:
the struct itself. But logically, the function returns as many
"values" as the struct contains.

Pete

--
I'm trying a new usenet client for Mac, Nemo OS X.
You can download it at http://www.malcom-mac.com/nemo



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.