Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between type? and Nullable<type>?

Tags:

c#

.net

nullable

In C# are the nullable primitive types (i.e. bool?) just aliases for their corresponding Nullable<T> type or is there a difference between the two?

like image 210
MojoFilter Avatar asked Sep 11 '08 13:09

MojoFilter


People also ask

Is nullable type reference type?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

What is the difference between nullable and non-nullable?

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 do you mean by nullable type?

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type.

What is the purpose of a nullable 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 .


2 Answers

If you look at the IL using Ildasm, you'll find that they both compile down to Nullable<bool>.

like image 57
Steve Morgan Avatar answered Sep 18 '22 18:09

Steve Morgan


There is no difference between bool? b = null and Nullable<bool> b = null. The ? is just C# compiler syntax sugar.

like image 34
samjudson Avatar answered Sep 18 '22 18:09

samjudson