Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Nullable<T> considered a value type?

Tags:

.net

nullable

Have you ever tried to use the Convert.ChangeType() method to convert a value to a Nullable<T> type? Awkwardly, it will throw an InvalidCastException saying "Null object cannot be converted to a value type".

Try running this on your immediate window: ?System.Convert.ChangeType(null, typeof(int?))

For some obscure reason, Nullables are considered value types. For example, typeof(int?).IsValueType returns true.

For me, since Nullable<T> accept null, it's a class type, not a value type. Does anyone know why it would implemented differently?

like image 700
jpbochi Avatar asked Jan 08 '10 21:01

jpbochi


People also ask

Is nullable a value type?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

What is a nullable data type?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.

What is nullable and non nullable value type?

Nullable variables may either contain a valid value or they may not — in the latter case they are considered to be nil . Non-nullable variables must always contain a value and cannot be nil . In Oxygene (as in C# and Java), the default nullability of a variable is determined by its type.

What is the point of nullable reference types?

Nullable reference types are a compile time feature. That means it's possible for callers to ignore warnings, intentionally use null as an argument to a method expecting a non nullable reference. Library authors should include runtime checks against null argument values.


1 Answers

System.Nullable<T> is technically a structure, so it's a value type (the null value for a Nullable<T> is not exactly the same thing as a null reference. It's a boolean flag that denotes the lack of a value.) However, it's treated specially by the runtime and this makes it an awkward value type. First, it doesn't satisfy where T : struct type constraint (it doesn't satisfy where T : class either, for what it's worth). Second, it exhibits an interesting boxing behavior. Boxing a Nullable<T> will result in:

  • A null reference, if the value is null.
  • A boxed value of the underlying type if it actually contains a value. That is, in the statements:

    int? x = 2;
    object y = x;
    

    y is not a boxed Nullable<int>. It's simply a boxed int. You can unbox any boxed value of type T to Nullable<T> (and T, of course). Casting a null reference to Nullable<T> results in a null value (as you might expect).

This special treatment by the runtime makes nullable types work more similar to the way null works in reference types.

like image 174
mmx Avatar answered Sep 23 '22 15:09

mmx