
Interesting moments in C # (boxing unboxing)
In this article, we briefly go over the little-known features of boxing / unboxing.
Previous article about foreach
Previous article about Array
A typical question at the interview about packaging and unpacking is as follows - “What will happen when this code is run, and if it does not work, then how to fix it?”.
Test code:
The answer may be the following - “When unpacking, the first operator is not a type cast but a type unpacking, respectively, it must correspond to the type of the value in the packed form.”
Correct answer:
This is usually considered the right answer, but it’s not quite right ...
Imagine a person’s surprise when you write him another correct option.
Second correct answer:
Let me remind you that enum is not a fundamental type and does not inherit it; it is a structure containing a fundamental type (base). This suggests that .NET has explicit support for such unpacking. It is also easy to verify that the decompression does not use the explicit and implicit conversion operators, and the IConvertible interface and its type cannot be expanded from an alien type.
When unpacking an enum, its base type is used and the next unpacking will not work.
Wrong option:
Unpacking for enums is extremely weak.
Unpack int from enum:
Unpack one enum from another:
Unpacking also supports Nullable types, which seems more logical.
Unpacking a Nullable type from normal:
Unpacking the regular type from Nullable:
Let me remind you that Nullable is a structure with one generalized type of value and is designed to store data and the flag of data presence. This suggests that C # has explicit support for decompressing Nullable types. In newer versions of C #, alias "?" Appeared for this structure.
Nullable:
Equivalent entries:
Thank you all for your attention!
Previous article about foreach
Previous article about Array
A typical question at the interview about packaging and unpacking is as follows - “What will happen when this code is run, and if it does not work, then how to fix it?”.
Test code:
object box = (int)42;
long unbox = (long)box;
The answer may be the following - “When unpacking, the first operator is not a type cast but a type unpacking, respectively, it must correspond to the type of the value in the packed form.”
Correct answer:
object box = (int)42;
long unbox = (long)(int)box;
This is usually considered the right answer, but it’s not quite right ...
Unboxing and Enum
Imagine a person’s surprise when you write him another correct option.
Second correct answer:
public enum EnumType { None }
...
object box = (int)42;
long unbox = (long)(EnumType)box;
Let me remind you that enum is not a fundamental type and does not inherit it; it is a structure containing a fundamental type (base). This suggests that .NET has explicit support for such unpacking. It is also easy to verify that the decompression does not use the explicit and implicit conversion operators, and the IConvertible interface and its type cannot be expanded from an alien type.
When unpacking an enum, its base type is used and the next unpacking will not work.
Wrong option:
public enum EnumType : short { None }
...
object box = (int)42;
long unbox = (long)(EnumType)box;
Unpacking for enums is extremely weak.
Unpack int from enum:
public enum EnumType { None }
...
object box = EnumType.None;
long unbox = (long)(int)box;
Unpack one enum from another:
public enum EnumType { None }
public enum EnumType2 { None }
...
object box = EnumType.None;
long unbox = (long)(EnumType2)box;
Unboxing and nullable
Unpacking also supports Nullable types, which seems more logical.
Unpacking a Nullable type from normal:
object box = (int)42;
long unbox = (long)(int?)box;
Unpacking the regular type from Nullable:
object box = (int?)42;
long unbox = (long)(int)box;
Let me remind you that Nullable is a structure with one generalized type of value and is designed to store data and the flag of data presence. This suggests that C # has explicit support for decompressing Nullable types. In newer versions of C #, alias "?" Appeared for this structure.
Nullable:
public struct Nullable where T : struct
{
public bool HasValue { get; }
public T Value { get; }
}
Equivalent entries:
Nullable value;
int? value;
Thank you all for your attention!