By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Structures v/s Classes

P: n/a
Hi,

can anyone give specific examples where structs are preferred to classes?
Feb 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Kiran A K a écrit :
Hi,

can anyone give specific examples where structs are preferred to classes?


I'm not a master of code, but I use struct for grouping variables withoutneed
of methods.

Like Event data and more generally composite parameters.

cyrille

Feb 3 '06 #2

P: n/a
# Cyrille37 # wrote:
can anyone give specific examples where structs are preferred to classes?


I'm not a master of code, but I use struct for grouping variables without need
of methods.

Like Event data and more generally composite parameters.


IMO, that's not a good way of deciding whether to use a struct or a
class. What you should be looking at is the semantics of structs and
classes, and that doesn't affect whether or not you have methods.

For me, the questions are (at least):

1) Is this a sort of low level "atomic" type? That would *suggest* a
value type (struct)
2) Is the data potentially large? (Direct data only) That would
*suggeste* a reference type (class)
3) Do you need the type to be mutable? That would almost always suggest
a reference type.

It's more than that, and you tend to get a feel for it, but those are
good starting questions IMO. The "default" position should almost
always be to use a class. I very rarely introduce my own structs.

Jon

Feb 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.