When to use class and when structure

  • Thread starter Thread starter Bhuwan Bhaskar
  • Start date Start date
Bhuwan said:
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
 
Bhuwan said:
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.
 
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.
 
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
 
Kevin said:
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
 
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
 
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 said:
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 said:
Hi,

When to use class and when structure in our application?

Thanks,

Bhuwan
 
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
 
Back
Top