During C# interviews (as the interviewer), I commonly ask about value types and reference types. Put concisely (more than accurately): Value types are things like enums, bools and ints and custom structs. They are stored on the stack, and are passed by value. Reference types refer to things inheriting from System.Object. They are stored on the heap. These are very important foundational concepts programmers should understand.
But there's another question I occasionally ask, more out of curiosity if they know the answer. It's not important because programmers can write a lot of C# not knowing this interesting piece of fact. The question is...
What's the difference between an int and a struct?
Short answer: ints are structs
Long answer
According to this documentation, there are only two categories of value types:
Structs can be broken into these categories:
- Numeric types: ints, floats, decimals
- bool
- User defined structs
These all are derived from System.ValueType
.
So there's you answer
It turns out that ints (and floats, decimals and bools) all derive from structs. In fact in the documentation here, it actually refers to Int32 as a struct.
So now you're ready for that interview question...