views:

761

answers:

7

I like using structs a lot.

So after reading this article, are there any other concerns I should have against using them all the time?

See Also:

+2  A: 

They don't fit into an Object Oriented programming paradigm like classes do. They are good for small data structures, but I use classes for anything beyond that.

Ed Swangren
A: 

I think main purpose of a struct - to keep only variable types. If you keep some classes into struct - you're wrong.

abatishchev
A: 

Use of structs should be limited to when all you really need is a small data structure. (as you read). I would only really use them for the smallest of datastructures like coordinates, offsets, and sometimes for graphics.

In many ways, you can think of structs in C# as being like scaled - down classes. They are basically the same as classes but designed more for cases where you simply want to group some data together. They differ from classes in the following ways: Some things to keep in mind

  • Structs are value types, not reference types. This means they are stored either in the stack or in- line (if they are part of another object that is stored on the heap) and have the same lifetime re-strictions as the simple data types.
  • Structs do not support inheritance.
  • There are some differences in the way constructors work for structs. In particular, the compiler always supplies a default no - parameter constructor, which you are not permitted to replace.
Paperino
A: 

Ask yourself the following questions about the set of data you're modeling with a struct:

  • Might it ever need to have any get/set logic?
  • Might it ever need to store ANY logic that is endemic to the data?
  • Might it need to inherit another set of data?
  • Might another set of data need to inherit this one?

I think if you can heartily answer "no" to all these questions, then there's no good reason not to use a struct. I think people use static subclasses in certain situations where a struct would be more than good enough.

danieltalsky
Could you go into more detail about point 2? There's nothing wrong with a struct having logic associated with it.
Jon Skeet
Well, the key word here is "endemic" vs. associated. I guess this isn't that clear, but what I meant was: might your struct need methods? I mean, classes are basically structs + endemic actions (well, ok, they're more than that, but that's what I was getting at)
danieltalsky
+5  A: 

I almost never define custom structs. There just aren't that many natural value types around, IMO.

In particular, I would think very, very carefully before defining a mutable struct, especially if it mutates via an interface implementation. Mutable structs behave in ways which people don't expect at all, leading to code which is hard to understand.

I think it's worth reading "Choosing Between Classes and Structures" from "Design Guidelines For Developing Class Libraries".

In particular:

Do not define a structure unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).

  • It has an instance size smaller than 16 bytes.

  • It is immutable.

  • It will not have to be boxed frequently.

Do you really develop types with all of those characteristics frequently?

Jon Skeet
+7  A: 

You should make the following considerations about structs:

  • structs should be immutable (mutable structs are not intuitive and unpredictable)
  • structs always have a default (public parameterless) constructor that cannot be changed
  • struct size should not exceed 16 bytes
  • the Equals and GetHashCode methods should be overriden for better performance
  • implementing the IEquatable<T> interface is recommended
  • redefining and == and the != operators is also recommended
Michael Damatov
What is the reason for limiting the size to 16 bytes?
SkippyFire
As far as I am aware, 16 bytes is the size of the reference to a reference type. Therefor you get no memory benefits when passing structs around or using them.Although I will say. I dont understand all this <em>you should use</em> immutable struct stuff. Structs are just structs and if they are what you need, I think they are what you should use.
Barry Jones
Mutable structs: consider the following multable `struct Point { public int X, public int Y }`. When it's used in a collection (e.g. `List<Point>`) iterating and modifying it would not update the value in the collection: `foreach(var p in points /* points is Point[] */) { p.X = 3 }`, because getting an element from the collection just gives you a *copy* (or a clone) of the element. The same is valid for passing struct values as method arguments.
Michael Damatov
Hi Michael, it's not that I dont understand the issues (if you can call them that) with structs. What you have stated to me, is what I consider expected behaviour. Therefor, if it is expected I wouldn't expect the values to be modified and wouldn't be supriprised if they were not.
Barry Jones
A: 

A class is a lot simpler to implement correctly than a struct. If you implement a struct incorrectly it can give you some unexpected errors.

A struct should not be larger than 16 bytes, or you lose most of the performance benefits.

A struct is intended to be a value type, representing a single entity of some kind.

A struct should be immutable. That means that you never change one of the properties in a struct. If you want a struct value that is different you create a new value.

Guffa