Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

C# - Is this a good or bad idea to use a struct

This topic is 3176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a class object that has 10 or so member variables that need to be initalzied upon creating of that object. In c++ I usually used a struct to identifiy these set of variables as one object. For example struct Time { int hours; int minutes; int seconds; // ext }; I would declare an object of that that struct and then fill its values in like Time t t.hours = 10; Finally I would then pass that struct along to the orginal class object in need of those variables. In C# I am not sure if this is a bad idea. Any opinions Regards Chad

Share this post


Link to post
Share on other sites
Advertisement
You don't need to do that in C#, since it has the special "object initializer" syntax. Like so:

class Time
{
public int Hours;
public int Minutes;
public int Seconds;
// .. etc ..
};

var t = new Time() {
Hours = 1,
Minutes = 2,
Seconds = 3,
// ...
};

Share this post


Link to post
Share on other sites
If a set of properties can be grouped together logically, then using a class (reference type) or structure (value type) is sensible (eg a DateTime or TimeSpan would often be used in your example). If they can't be grouped together logically then I wouldn't bother, and just pass the values as distinct parameters.
Quote:
Original post by Codeka
You don't need to do that in C#, since it has the special "object initializer" syntax. Like so:

*** Source Snippet Removed ***
For what it's worth, object initialisers only work on properties, not fields. Edit: I'm an idiot.

[Edited by - benryves on October 13, 2009 6:09:08 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
For what it's worth, object initialisers only work on properties, not fields.
I just tried it to double-check, and it seems to work fine over here..? (.NET 3.5, Visual Studio 2008)

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Quote:
Original post by benryves
For what it's worth, object initialisers only work on properties, not fields.
I just tried it to double-check, and it seems to work fine over here..? (.NET 3.5, Visual Studio 2008)
You're quite correct. Whoops. Sorry! [embarrass]

Share this post


Link to post
Share on other sites


I think it's quite subjective, but follow this recommendation:

The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

Share this post


Link to post
Share on other sites
Passing that many parameters to a method or a constructor does call into question the design of that class. I'd be wondering if the class is just used as syntactic sugar for a storage container or whether it does too many things at once.

Consider for example Robert C. Martin's "Clean Code" where he talks about monadic, dyadic and triadic methods. 4 Parameters are already out of his comfort zone.

If a class really requires that many parameters to be constructed, maybe you could apply "convention over configuration" - use default behavior that covers most normal use cases and let the user change the configuration only if required.

Or create a builder for the class that will construct an instance following a series of method calls setting up the instance (and also combining the parameters into logical sets related to a single aspect or behavior of the instance)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!