#569 – Assignment Compatibility
April 25, 2012 2 Comments
The idea of assignment compatibility in C# is the idea that you can store a value that has a particular type into a storage location (variable) of a different type without losing any data. (The conversion is “representation-preserving“).
So we can say that type T is assignment compatible with type U if we can store values of type T into variables of type U.
For value types, a type T will typically be assignment compatible with another type U, if T can represent a subset of the values that U can represent. T can be thought of as “smaller” or “narrower” than U.
// byte is assignment compatible with ushort byte n1 = 123; // byte: 0-255 ushort n2 = n1; // ushort: 0-65535
As you’d expect, a type is always assignment compatible with itself:
byte n1 = 123; byte n2 = n1;