Structure vs Class

T

Tim

Hi Gurus,
I am newbie and little confused with Classes(Ref type) and Structures(Value
type).
I know how to use both classes and Structures and also know the difference
between value type and ref type, but I don't see much use of structures.
Please help me by answering the question below.

1. Is there anything a Structure can do and Class cannot.
2. What is the advantage of Structure over Classes
3. When should we use Structure.

Please clear my doubt. I'll appreciate any help you can provide
Thanks, Tim.
 
J

james

Hi Gurus,
I am newbie and little confused with Classes(Ref type) and Structures(Value
type).
I know how to use both classes and Structures and also know the difference
between value type and ref type, but I don't see much use of structures.
Please help me by answering the question below.

1. Is there anything a Structure can do and Class cannot.
2. What is the advantage of Structure over Classes
3. When should we use Structure.

Please clear my doubt. I'll appreciate any help you can provide
Thanks, Tim.

There isn't much value to structures except to improve performance.
Most of the value types in .Net are about the size of a pointer so it
makes sense for Int32 and DateTime to go on the stack instead of the
managed heap. I don't like structs because you cant set them to null
(so int i = null; won't compile). There are some gotchas with
valuetypes as well so I would just make everything a class.
 
G

Guest

I personally find a lot of value in value types (excuse the pun)
Imagine if there where no value types and you did some control manipulation
like this

textbox1.Size = textbox2.Size;

then later on did

textbox1.Size.Height *= 2;

This would double the height of textbox1 and (probably unintentially)double
the height of textbox2. The handy thing about Value types is that every
reference is to a different copy so assigning one from the other copies it.
This is handy when you want this and is also more performant when passing
them as parameters to functions as long as they are lightweight as
recommended. They should represent a complete piece of information in the
same way as the Size, Point, Rectangle structures do.
Structures all help with encapsulation as with the textbox example above,
giving out a reference to a Size class would enable some body to change the
height of the textbox without using the height property. With structs, this
isnt possible as when you get the return value from the Size property, it is
a copy of the textboxes internal data, not a direct reference to it.

I'm not a great technical authore but here is a link to a good document on
the diferences in practice:

http://www.awprofessional.com/content/images/0321245660/items/wagner_item6.pdf

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top