Simple, Quality, Awesome Software

Structs Quiz

Below are the answers to the Structs Quiz, along with a brief discussion.

1. Structs are value types. Their counterpart, classes, are reference types.

2. False. While it is easy to change the struct keyword to the class keyword or vice-a-versa, because of the difference in value and reference types, making this kind of a change will potentially have a huge impact on the functionality of your code. Furthermore, classes and structs support different things (nullable types and inheritance to name a few) and if you’re using any of them, you may not be able to simply switch.

3. False. They’re not required to be immutable, but they generally should be created in a way that they’re immutable.

4. False. You should also try to make classes immutable when it makes sense, but because they’re reference types, the impetus for doing so is less. The string type is a reference type and it is immutable.

5. False. Most of them are, but the string and object types are both reference types. Arrays are also reference types.