Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Nullable Types and the Value property

Tags:

I'm a little unclear on when/if the Value property on nullable types must be used when getting the value contained in a nullable type. Consider the following example:

int? x = 10;  Console.WriteLine("The value of 'x' is " + x.Value); Console.WriteLine("The value of 'x' is " + x); 

Both of these return the same value (10).

However, if I initially set x to null, the first Console.WriteLine throws an exception and the second one does not.

So, my question is this. What is the point of using the Value property? It doesn't appear to be needed to get the actual value (even if it's null) and will throw an exception if the value is indeed null.

like image 519
Randy Minder Avatar asked Mar 23 '11 13:03

Randy Minder


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

It is needed usually - just not in your particular case. The type of x is Nullable<int>, not int - and there's no implicit conversion from Nullable<T> to T.

Let's look at what's happening in your example though. Your final line is being converted into:

Console.WriteLine(string.Concat("The value of 'x' is ", x)); 

That's boxing x, which will result in either a boxed int or a null reference... both of which are handled by string.Concat.

When you're not converting to a string via string concatenation, e.g. if you wanted:

int nonNullable = x.Value; 

then you do have to use the Value property - or an explicit cast, or possibly a null coalescing operator, or a call to GetValueOrDefault:

int nonNullable = (int) x; int alternative = x ?? 20; int andAnother = x.GetValueOrDefault(20); 
like image 72
Jon Skeet Avatar answered Sep 19 '22 15:09

Jon Skeet


x and x.Value have different types.

    static void Meth(int i)     {         int? x = 5;         Meth(x);         Meth(x.Value);     } 

Second line doesn't compile.

So reason of using x.Value is obvious - you need to call it when you need the actual value. And NullReferenceException is reasonable, because null doen't correspond to value of value type.

like image 29
Andrey Avatar answered Sep 20 '22 15:09

Andrey