Arrays of classes or structs, which is best

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?

Thanks
Macca
 
Read recomendations there http://www.jaggersoft.com/pubs/StructsVsClasses.htm
I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
I don't find that reference very helpful in deciding whether to use
struct or class. It is very good at covering the small-scale
differences between the two, but doesn't really give a high-level view
of when one is more appropriate than the other.
 
Use a class. In this situation you absolutely don't want a struct. I
can think of no good reason to have a struct holding a collection. The
..NET Framework's Queue is a class... and there's a good reason for
that.

On top of this, you're storing them in an aggregate structure, which
(at least in .NET 1.1) will cause counterintuitive behaviour if you use
struct. (Not to say that storing structs in ArrayLists or other
collections is wrong... just that it produces behaviour that is
difficult to understand.)

Use a class. Even better, just use the Queue class from the Framework.
 
Hi,

I have a number of queue structures. I want to have multiple instances of
these,

i.e Instance 1 Instance 2 Instance 3 ...
------------ ------------ ------------
queueA queueB queueC

I was thinking of usind a structure or a class to store the queues and then
having an array of theses structures or classes.

As I am only storing queues what is the best object to hold them, a class, a
structue or another data structure?

Thanks
Macca

A struct is a value type (on the stack); a class is a reference type
(on the heap). Only use structs in specific cases (see
http://msdn.microsoft.com/library/d...genref/html/cpconvaluetypeusageguidelines.asp).

In your case, a class would be a good choice.
 
Back
Top