design question : struct or class

P

phoenix

Hello,

I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

Since there's no real business logic my choice would be structs. But looking
at the size of the objects in memory I would think they are a bit large to
put on the stack and should be better of on the heap. Which would mean I
should use classes.

Any ideas about what would be best?

TIA

Yves
 
J

Jay B. Harlow [MVP - Outlook]

P

Peter Rilling

It sounds like your object has the potential of being quite large. Remember
that structures are allocated on the stack, which is finite. I don't know
the stack size off the top of my head.
 
J

Jeff Louie

Pheonix... To be clear, if you add structures to a .NET _collection_,
they
will be boxed anyway.

Regards,
Jeff
array of max 255 of these classes/structs (in most cases it will be
less
then 5 however)<
 
J

Jeff Louie

Peter... To be clear, structures can be inlined if they are part of a
class or
?array.

Regards,
Jeff
 
C

codymanix

I've got a design question. I need to keep track of some variables and I
am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.
 
O

ozbear

You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

Rubbish....anyone saying "always" do or don't do X is practicing
religion, not programming. Structs have their proper place in the
programming paradigm, just as classes do. Knowing which to use and
when defines the difference between a programmer and a zealot/ostrich.

Oz
 
G

Guest

Use them whatever best way suits you. Scew the fanboys.

Same for any aspect of software. Do what works best for you.
 
P

peter x

Hi there,

The main difference is that a 'class' is an object type and a struct is a
'value' type. When it comes to assignment and parameter passing its
important to know the difference:


Assignment
-------------
myObject o1 = new myObject()
myObject o2 = new myObject()
o1 = o2

Objects are 'reference' types and are really pointers to an address in
memory where values are stored. In the above example space is allocated for
two myObject types. o1 points to the first myObject type and o2 points to
the second myObjects type. Because objects are reference types, its not the
values held in o2 that are copied into o1 but rather the address in memory
that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
pointing both o1 and o2 at the same place in memory and changes to o1 will
change o2 and vice versa. If no references remain to the memory allocated
for the original o1 object it will eventually be garbage collected.

myStruct s1 = new myStruct()
myStruct s2 = new myStruct()
s1 = s2

Structs are 'value' types and are very much like ints or floats. In the
above example space is allocated for s1 and s2. s1 is the first myStruct
type and s2 is the second myStruct type. Because structs are value types,
its not the reference for s2 that is copied into s1 but the values.
Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
memory with the same values. Sometimes this is what you want and sometimes
it isn't. For example, If your struct type contains a nested object it isn't
the values of the nested object that are copied but rather the reference. In
the case of 's1 = s2', you will have two seperate blocks of memory which
contain the same values but the object reference in both will point to the
same place in memory. To find out more about avoiding this problem look for
help and shallow vs deep copying.


Parameter Passing
---------------------

When objects are passed to a method, they are passed by reference. This
means that any changes made to the object within the method will remain once
the method had been completed.

When structs are passed to a method, they are passed by value. This means
that the system makes a copy is made and changes made to the struct will not
remain when the method is complete. You can get round this by returning the
copied struct and assigning it to a variable but there is an obvious over
head in working with copies.


Summary
----------

The choice of whether to use a class or a struct is entirely down to you,
both have there place. However, because of the overheads involved in passing
by value and the added complexity of nested objects, I uses classes most of
the time. I only use structs when the offer me exactly what i need. I hope
this helps and that my explanation was too confusing and if I am wrong on
any point my apologies in advance.


Happy coding, Peter




codymanix said:
I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)

You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
R

rhavaldar at shodaka dot net

I would suggest grouping your 'data' into structs/classes (based on the
your need for value type/reference type. You would potentially
end up w/ many of these structures and not just 'one' global placeholder.

For more, check out the structural design patterns @ c2 -
http://c2.com/cgi/wiki?CategoryStructuralPatterns

- raghu
 
J

Jon Skeet [C# MVP]

When objects are passed to a method, they are passed by reference.

No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
 
P

peter x

I stand corrected :) Although I'm not sure I totally understand your
response. Can you send me an example to help me clarify. Apart from this
point was the rest of the assessment correct? specifically the point about
the overhead involved in passing by value. I may need to reassess how I
program

Cheers, Peter
 
J

Jon Skeet [C# MVP]

peter x said:
I stand corrected :) Although I'm not sure I totally understand your
response. Can you send me an example to help me clarify.

Sure.

Here's what pass by value means:
The value of the expression in the actual parameter (i.e. in the
calling code) becomes the value of the new variable which is the formal
parameter (i.e. in the method itself).

Here's what pass by reference means:
Only a variable (non-read only) can be used as a pass by reference
parameter, and the formal parameter variable in the called method is
effectively aliased with the actual parameter - they are different
names for the same memory location, so changing the value of one
changes the value of the other.

Note that nowhere in those definitions is the type of the parameter
(i.e. reference type or value type) used. Both value type parameters
and reference type parameters can be passed by value or by reference,
and in both cases the default is "by value". The only twist here is
understanding that the value of a reference type expression (or
variable) isn't an object - it's a reference. So, if I do:

string x = "hello";
Foo (x);
....
void Foo (string y)
{
....
}

the string itself isn't passed - the string *reference* (the value of
x) is passed by value.

For more examples etc, have another look at the article I linked to
before.
Apart from this point was the rest of the assessment correct?

As far as I saw, yes - I only skimmed it though. (The usual terminology
is reference type rather than object type though.)
specifically the point about
the overhead involved in passing by value. I may need to reassess how I
program

Unlikely. You should indeed by using classes most of the time, and
passing a reference by value is indeed cheaper than passing a large
value type value.
 
R

Ravichandran J.V.

Yes, if the memory that the object is going to occupy is large, the
usual choice is a class but, if the object is going to contain only
data-members, then it is beter to use a struct as a class is generally
resolved as a container for logic apart from data-members.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
J

Jeff Louie

As Jon has stated, objects, by default, are passed by value. Structures
have the overhead of value semantics, as I see it, just like C++
classes,
but without the benefit of C++'s deterministic destructors. Thus the
limited usefullness of structures in C#.

Regards,
Jeff
 
J

Jon Skeet [C# MVP]

Jeff Louie said:
As Jon has stated, objects, by default, are passed by value.

I didn't quite state that - I stated that *parameters* are passed by
value by default. Reference type objects are never passed in any way,
as the type of an expression is never a reference type object, only a
reference.
 
L

L#

You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.

You should use structs if they are very small. They live on the stack
and are therefore faster. But if they are too big, the advantage of
being faster dissappears. If they become too big, the framework will
put them on the heap anyway, even if you declared it as a struct.

So, is they are very small, use structs, else use classes.
 
L

L#

No they are not. "Pass by reference" has a very specific meaning, and
it *doesn't* apply here (unless you use the ref modifier). There is a
big difference between a reference being passed by value (which is what
actually happens) and a parameter itself being passed *by* reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

Being objects, they are passed by value; meaning that the pointer to
the object is passed by value. Eventually it has the same effect as
being passed by reference. Placing the ref keyword in front of it,
won't make a difference.
 
J

Joe Mayo [C# MVP]

L# said:
You should use structs if they are very small. They live on the stack
and are therefore faster. But if they are too big, the advantage of
being faster dissappears. If they become too big, the framework will
put them on the heap anyway, even if you declared it as a struct.

So, is they are very small, use structs, else use classes.


Hi L#,

Structs reside on the stack if they are parameters or local variables,
otherwise they are stored on the heap. The best way to determine whether
the size of the struct is affecting performance is to benchmark your
application. I've never seen or heard anything that would make me believe
that the size of a value type determines whether they reside on the stack or
on the heap.

Jon Skeet wrote a nice article that explains this better:
http://www.yoda.arachsys.com/csharp/memory.html.

The reason you would use a struct instead of a class is if you need
value-type semantics, similar to int, float, or DateTime.

Joe
 

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