This question is more about adding a ?
to a value type than about int?
In C# an int
is a value type.
Is int?
a value type or a reference type?
In my opinion it should be a reference type as it can be null
.
Int is certainly not a reference type in C#. It's a numerical struct, which is a value type. When talking about C#, it is incorrect to say int is a reference type.
NET maintains an understanding of what the object is (it knows it's an int, which isn't a reference type).
Array, which itself is derived from System. Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.
Examples of reference data types are class, Arrays, String, Interface, etc. Examples of primitive data types are int, float, double, Boolean, long, etc.
int?
is equivalent to Nullable<int>
which means it is a struct.
So that means it is a value type.
In my opinion it should be a reference type as it can be null.
Your assumption is wrong. From documentation;
Nullable structure represents a value type that can be assigned null. The Nullable structure supports using only a value type as a nullable type because reference types are nullable by design.
So it has 2 values;
Nullable<T>.HasValue
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With