|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
When to use class and when structureHi,
When to use class and when structure in our application? Thanks, Bhuwan Bhuwan Bhaskar wrote:
> Hi, I think it would be hard, if not impossible, to come up with a good, > > When to use class and when structure in our application? 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 Bhaskar wrote:
> 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. Thanks all
Bhuwan Show quote "Bhuwan Bhaskar" <k***@gmail.com> wrote in message news:eyO5O1eFIHA.4752@TK2MSFTNGP04.phx.gbl... > Hi, > > When to use class and when structure in our application? > > Thanks, > > Bhuwan > 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 -- Show quoteHTH, Kevin Spencer Chicken Salad Surgeon Microsoft MVP "Bhuwan Bhaskar" <k***@gmail.com> wrote in message news:OL7nIfhFIHA.3548@TK2MSFTNGP06.phx.gbl... > Thanks all > Bhuwan > > "Bhuwan Bhaskar" <k***@gmail.com> wrote in message > news:eyO5O1eFIHA.4752@TK2MSFTNGP04.phx.gbl... >> Hi, >> >> When to use class and when structure in our application? >> >> Thanks, >> >> Bhuwan >> > > Kevin Spencer wrote:
> Another consideration is that a class is a reference type while a structure Just be aware that:> 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. 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. -- Show quoteHTH, Kevin Spencer Chicken Salad Surgeon Microsoft MVP "Peter Duniho" <NpOeStPe***@NnOwSlPiAnMk.com> wrote in message news:13i1nbm3oguio25@corp.supernews.com... > 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 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. Show quote "Bhuwan Bhaskar" <k***@gmail.com> wrote in message news:eyO5O1eFIHA.4752@TK2MSFTNGP04.phx.gbl... > Hi, > > When to use class and when structure in our application? > > Thanks, > > Bhuwan > > 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 Show quote "Andrew Faust" <and***@andrewfaust.com> wrote in message news:A4864C44-88AA-4000-9D89-B6649B7B7038@microsoft.com... > 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" <k***@gmail.com> wrote in message > news:eyO5O1eFIHA.4752@TK2MSFTNGP04.phx.gbl... >> Hi, >> >> When to use class and when structure in our application? >> >> Thanks, >> >> Bhuwan >> > In article <u7j5DAGGIHA.4***@TK2MSFTNGP06.phx.gbl> "Bhuwan
Bhaskar"<k***@gmail.com> wrote: >> struct is if I have a function that needs to return more than 1 By putting the values into a struct, and then returning the struct.>> value. For > Kindly explain how a function can return more than one value? 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 |
|||||||||||||||||||||||