2

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.

ckv
  • 10,539
  • 20
  • 100
  • 144
  • @GrantWinney That does seem to be the question, though I think you have overcomplicated it. `String myStr;` vs `String myStr = null;` would satisfy the condition. – MaxPRafferty Jan 03 '14 at 16:13

4 Answers4

4

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 Nullalbe<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 Nullalbe<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.

Servy
  • 202,030
  • 26
  • 332
  • 449
2

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.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • 1
    But isn't Nullable itself a non-nullable type? – MaxPRafferty Jan 03 '14 at 16:15
  • 1
    @MaxPRafferty He states that in his first sentence. – Adam Houldsworth Jan 03 '14 at 16:15
  • @MaxPRafferty `Nullable` is a structure so its a value type and cannot be null itself. however its `Value` member may be null. – Hossein Narimani Rad Jan 03 '14 at 16:16
  • The `Value` of a `Nullable` can *never* be null. It will always be a struct, and structs can't ever be null, which is the whole reason for having a `Nullable` type in the first place. – Servy Jan 03 '14 at 16:16
  • @Servy What about `Nullable.Value`? – Hossein Narimani Rad Jan 03 '14 at 16:18
  • 1
    @HosseinNarimaniRad What about it? It's a struct. it's non-nullalbe. If it were nullable, there'd be no reason to use `Nullable` in the first place. When a `Nullable` value represents `null` the `Value` can have anything; it's garbage data. Generally it'll have the default value, although it doesn't really matter what bits are in there as it can't be read. – Servy Jan 03 '14 at 16:19
  • 2
    @HosseinNarimaniRad: The `Value` property of `Nullable` is `T`, which is a value type, so that's not null either. You can compare a `Nullable`to null, but it isn't null (see [this answer](http://stackoverflow.com/a/676089/593627)). – George Duckett Jan 03 '14 at 16:20
  • Right, but that isn't unique to the Nullable (big N) struct, or any struct, or any non-nullable (little n) type for that matter. It seems unrelated to the question, which is what the difference is between a nullable type "not having a value" and a nullable type being "null". – MaxPRafferty Jan 03 '14 at 16:23
0

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.

Community
  • 1
  • 1
Chris
  • 8,268
  • 3
  • 33
  • 46
0

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.

MaxPRafferty
  • 4,819
  • 4
  • 32
  • 39