I have a linear array of structs. These are constant structs and is a way of holding a simple table.

Now though I need to add another "column" to the table (another member in the definition of the struct) and I'm wondering if I'd be better using a class rather than a struct.

Since both structs and classes have constructors I'm wondering which is best to store fixed arrays of data?

Recommended Answers

All 4 Replies

It's really the difference between a value type (struct) and reference type (class). For types that are suitable to both, I tend to prefer reference types for consistency. But that's more of a stylistic choice than anything.

Someone has pointed out to me that if I ever wanted to pass the struct or class as a parameter to a function then a class would be better. As you point out the struct would get passed as a value, and if the struct got bigger so the passing would become more heavy.

I don't yet want to pass elements of this array to functions, but I may do in the future so I've changed the elements into a class.

As you point out the struct would get passed as a value, and if the struct got bigger so the passing would become more heavy.

This is true, but there's also the question of how heavy the object is and how often it's copied around. If it's not that heavy relative to a reference or you don't pass it often, the performance difference will be negligible.

I have a feeling this struct may grow with the complexity of the program, so I've turned it into a class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.