ckv
ckv

Reputation: 10838

Difference between Nullable types "not having a value" and "being Null" in C#

What is the difference in the terms

  1. Not having a value

and

2.Being Null

in Nullable types in C#.

In otherwords when do we say a type does not have a value and when do we say the value is null.

Upvotes: 1

Views: 2260

Answers (4)

Servy
Servy

Reputation: 203825

Nullable<T> is a struct, and, being a struct, it cannot ever actually be null. The only actual data stored in a Nullable<T> is an underlying value of type T and a boolean HasValue flag. A Nullable<T> value in which HasValue is false can be thought of as being null even though it technically is not null.

There is some compiler magic such that someNullable == null can be called, and the result of that expression is actually the same as someNullable.HasValue. You can also assign null to a Nullable<T>. What this will actually do is create a new Nullable<T> struct where HasValue is false. (Interestingly, it's not possible for you to do that yourself if you were to write your own type.)

There is some more compiler magic such that if you box a Nullable<T> it doesn't actually box the nullable type. If HasValue is actually true then it will pull out the underlying value and box that. If HasValue is false then it will just box null. When unboxing something to a Nullable it then does the reverse; creating a Nullable<T> struct based on whether it's null or not. (This is another thing that you couldn't do with a custom type.)

Because of these special compiler features it does a fairly good job of making it appear as if Nullable can actually be null, but the reality is that it is not actually null.

Upvotes: 4

MaxPRafferty
MaxPRafferty

Reputation: 4987

If we are discussing terms, let's be clear with language. As many of the other answers have said, in c#, when a non-nullable type is made to be nullable using the ? operator, it becomes the Nullable struct, where T will always be a value type.

Ref: http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

see Servy's answer for a good explanation of this.

However, once any type is nullable (regardless of if it is a reference type or has been made to use the Nullable struct) it would be correct to say that if the type has no value, the type is null, and vice versa. From the above Microsoft documentation:

A type is said to be nullable if it can be assigned a value or can be assigned null, which means the type has no value whatsoever.

Upvotes: 0

Chris
Chris

Reputation: 8656

It's been mentioned that the generic nullable type, Nullable<T> (where T must be a value type) has the important properties:

bool HasValue // Indicating that a real value is present, or if the instance should be considered null
T Value        // Your value, if present. (non-nullable)

So you could conceivably have these cases for a Nullable<int> myInt:

HasValue  |  Value  |  Represents
------------------------------
false     |   0     | No Value / null
true      |   0     |    0
true      |   3     |    3

In the first case, HasValue indicates that the myInt has no value, and represents the null state; a comparison for equality with the null literal will be considered true:

(myInt == null) // true.

The nullable type itself will not actually be null (it's a struct, it can't be), but the compiler will resolve the the null comparison by querying HasValue, as mentioned in one of the other answers linked.

Upvotes: 0

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32521

A Nullable<T> itself cannot be null because its a structure. So you may find a Nullable<T> that follow the case: Not having a value but it is not null actually.

EDIT:

---------------------------- Not Having a Value case ------- Can be null case


Reference types ----(equals to can be null case)-------- Possible

Value types------------ Not Possible ---------------------------Not Possible

Nullable-----------------Possible --------------------------------Not Possible

Here, the sepration of Nullable and Value types do not mean Nullable is not a value type.

Upvotes: 2

Related Questions