Hi,
When to use class and when structure in our application?
Thanks,
Bhuwan
Peter Duniho - 24 Oct 2007 05:09 GMT
> 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
Göran Andersson - 24 Oct 2007 08:05 GMT
> When to use class and when structure in our application?
If in doubt, use a class.
Structures can be used for some small types, mostly for performance
reasons. If you implement an enumerator, for example, it might make
sense to make it a structure.
If you want to use structures, you should read up on how they work exactly.

Signature
Göran Andersson
_____
http://www.guffa.com
Bhuwan Bhaskar - 24 Oct 2007 09:25 GMT
Thanks all
Bhuwan
> Hi,
>
[quoted text clipped - 3 lines]
>
> Bhuwan
Kevin Spencer - 25 Oct 2007 11:39 GMT
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

Signature
HTH,
Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
> Thanks all
> Bhuwan
[quoted text clipped - 6 lines]
>>
>> Bhuwan
Peter Duniho - 25 Oct 2007 19:16 GMT
> 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
Kevin Spencer - 26 Oct 2007 12:11 GMT
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.

Signature
HTH,
Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
>> 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
[quoted text clipped - 19 lines]
>
> Pete
Andrew Faust - 25 Oct 2007 04:51 GMT
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.

Signature
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
> Hi,
>
[quoted text clipped - 3 lines]
>
> Bhuwan
Bhuwan Bhaskar - 27 Oct 2007 07:08 GMT
> 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
> 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
[quoted text clipped - 8 lines]
>>
>> Bhuwan
Peter Duniho - 27 Oct 2007 08:36 GMT
>> 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

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