Reputation: 23
While writing tests that involved manual type checking I came across the fact that null
does not evaluate as an instance of Nullable<>
types, e.g. for the type Nullable<int>
(which is the same as int?
) the following statement is false
null is int?
while assigning null
to int?
is valid. Why is this so?
Upvotes: 2
Views: 284
Reputation: 32740
Well, why should nullables be special? This doesn't stand for any reference type, period:
var isObject = null is object;
Will give you a compile time error:
Error CS0184: The given expression is never of the provided ('object') type
Nullable<T>
is a red herring here.
The type of null
is the null type and its assignable to any reference type or nullable type because there is an implicit conversion (§2.4.4.6).
This is
operator according to the spec works the following way (§7.10.10):
The is operator is used to dynamically check if the run-time type of an object is compatible with a given type. The result of the operation E is T, where E is an expression and T is a type, is a boolean value indicating whether E can successfully be converted to type T by a reference conversion, a boxing conversion, or an unboxing conversion. The operation is evaluated as follows, after type arguments have been substituted for all type parameters:
- If E is an anonymous function, a compile-time error occurs
- If E is a method group or the null literal, or if the type of E is a reference type or a nullable type and the value of E is null, the result is false. (...)
Bolded part for clarification.
Read this and this answer for more info.
Upvotes: 2
Reputation: 134
In sort:-by making any value type nullable,null wont get converted to that type or null is not of that type.In case of nullable if the variable is assigned to null, HasValue property of sturct becomes false and then boxing will return reference for null but it doesn't mean it is of that particular value type.
you need to look into implementation and working of
Nullable<T>
Fundamental Properties The two fundamental members of the Nullable structure are the HasValue and Value properties. If the HasValue property for a Nullable object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.
Boxing and Unboxing When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable object, not the Nullable object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable structure initialized to the underlying value. If the HasValue property of a nullable type is false, the result of a boxing operation is null. Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is null. When null is unboxed into a nullable type, the common language runtime creates a new Nullable structure and initializes its HasValue property to false.
i have just copied this line from Microsoft docs it will give you some idea about hole concept for reading entire documentation link is bellow
https://learn.microsoft.com/en-us/dotnet/api/system.nullable-1?view=netframework-4.7
Upvotes: 0
Reputation: 11357
What may be confusing is that null
can be a value and a type.
If you compare or assign null
the following implicit conversion is used to first convert the value to a Nullable
.
public static implicit operator Nullable<T> (
T value
)
If you are using is
not a value but a type comparison will be done. As null
is of type null
and not Nullable<T>
they aren't equal.
Upvotes: 0