开发者

Should I use a Struct? [duplicate]

开发者 https://www.devze.com 2023-01-21 19:27 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: When to use struct in C#?
This question already has answers here: Closed 12 years ago.

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 ad开发者_JS百科vice would be appreciated!

Thanks, Stuart


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.


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!


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!


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


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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消