tags:

views:

105

answers:

7

Possible Duplicate:
When to use struct in C#?

Hi, I am creating an application that has a class in C# that is purely for holding variables, it does nothing else but set and get these variables. I was wondering, for efficiency and good coding practice, if I should convert this class to a struct so that it is being used properly. I've never used structs before but have been looking into them however I am having some trouble getting it working. Any advice would be appreciated!

Thanks, Stuart

+3  A: 

If the collection of values model a value type (that is, something that doesn't have an identity of its own and two instances with the same values are considered the same) use a struct.

Otherwise, use a class.

Oded
Thanks, from this I will continue to use a class.
stuartmclark
A: 

Using structures in place of classes depends on the scenario on which you are working. Although you can use structures in place of classes but structures lacks the ability to implement access identifiers (such as Private, Public, Protected). If you are using classes just for holding variables, you can use structures as well, but if your class contains some private, protected or public methods or variables/properties, you can not use structures.

Hope this helps!

Vinod Maurya
struct *can* have private methods. "protected" methods however make no sense, as you can't derive from a struct.
Hans Kesting
A: 

The decision for struct or class should be based on "do I want reference type semantics or values type semantics?"

When you use a struct as parameter, the complete contents are copied. For a class just the reference is copied. So a "big struct" could have a performance penalty!

Hans Kesting
A: 

You use structs when:

  • Logically represents a single value
  • Has an instance size less than 16 bytes
  • Will not be changed after creation
  • Will not be cast to a reference type

Refere to MCTS 70-536

A_Nablsi
Sometimes it is desirable to have structs that are bigger than 16 bytes. With 'If you are not planning to reassign the instance after creating it.' you propably mean: 'You want a Type that is (should be) immutable'
dkson
Maybe you didn't like that sentence and neither do I, I think it is better now and that what MS recommendeds by structs.
A_Nablsi
A: 

In most cases, a class is preferred. Structs are passed by value, have no inheritance, etc. You mention efficiency, is this class likely to be a performance bottleneck ?

If you make a struct though, make sure to make it immutable

ohadsc
A: 

Hi,

I'm not sure that using a struct or not relates to good coding practise, but there are some performance benefits to using structs under certain circumstances. For example, MSDN suggests that a type which is under 16 bytes in size might be more efficiently handled by a struct than a class.

It's important to understand why a struct might be a better choice and how their memory is managed by the runtime. Libraries which must perform quickly with the minimum of overhead would consider using structs (such as a lot of the Math types in the XNA framework). There are also design issues like the fact that your struct always has a default constructor; if you want to make sure that your type can only be constructed with specific values then a struct isn't the best choice.

The long and short of it is, unless you have a very specific reason to be using structs over classes, just stick with classes.

If you want to press ahead with structs, what issues are you having with them?

elkdanger
A: 

It really depends on what you're trying to achieve. The fact that you currently have only getters and setters doesn't mean anything. For example, a type might be modelling the application settings read from a file. At some point you might wish to add a IsValid() or Normalize() methods. So in this case, you'd rather go with a class then a struct.

A struct should be used, when the type's identity is decided by the value of the fields. A good example from the .NET framework is Point, which has X and Y. The second thing is that structs should be cheap to pass to and from functions:

public bool IsInRange(Point point)
{
    // ...
}

Remember that point will be copied field by field here, so that should be fairly cheap.

Stefan