Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default(Nullable(type)) vs default(type)

In C#, is there a difference between default(Nullable<long>) (or default(long?)) and default(long) ?

Long is just an example, it can be any other struct type.

like image 930
Eugene Avatar asked Oct 12 '11 00:10

Eugene


People also ask

What is default of nullable type?

The default value of a nullable value type represents null , that is, it's an instance whose Nullable<T>. HasValue property returns false .

What is the difference between null and default?

The first NULL says that the column is nullable, i.e. accepts NULL . The second NULL (after DEFAULT ) is the default value. If you only have the default, but make the column reject nulls, then that default cannot be used.

Should I use default or null C#?

There's no difference. The default value of any reference type is null . MSDN's C# reference page for default keyword: https://msdn.microsoft.com/en-us/library/25tdedf5.aspx.

Should I use nullable reference types?

Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.


1 Answers

Well yes. The default value of a nullable or other reference type is null while the default value for a long or other value type is 0 (and any other members set to their defaults).

In this case:

default(Nullable<long>) == null default(long?) == null  default(long) == 0L 
like image 179
Jeff Mercado Avatar answered Sep 21 '22 09:09

Jeff Mercado