I wrote some code. It was simple code. I had a collection. I iterated over the collection and updated the values. I did this several times in an outer loop.

The code didn’t work.

Why didn’t it work? My collection was an array of Structs. When I pulled an item out in the for loop, it was copied. When I updated the copy, the changes went away at the end of the scope.

This bug survived because my simple use case actually meant the same value was stored back in at the end of a simple mathematical calculation, so I didn’t see a problem. But code has a way of changing, and when I tried to use this code with something where the math didn’t work out so cleanly it failed, and it was subtle. It took some debug-foo to find the issue until I realised it was a Struct.

My Struct is now Immutable.

In C#, your structs should be immutable.

If they are mutable, another developer will not be able to tell it is not a class, and they will have certain pre-conceptions when they read your code. They will see class types, and they will expect it to pass by reference, and mutate the same original instance. That other developer will probably be you, In a few months. This is a perfect example of why you need to use canonical techniques and idiosyncracies of the language you are coding in, too.

Or, just use a Class.