Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check against: null vs default()?

Tags:

c#

null

.net-3.5

I want to check if a reference type is null. I see two options (_settings is of reference type FooType):

if (_settings == default(FooType)) { ... } 

and

if (_settings == null) { ... } 

How do these two perform differently?

like image 667
lance Avatar asked Jul 12 '10 15:07

lance


People also ask

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.

What is default t in c#?

Default represents default value of T parameter in generics intructions. In several cases, the default keyword is an absolute unknown and we think it's unnecessary or its functionality is null. There are many development moments with the generics classes where the default keyword can be useful.

What does default () do in C#?

The default keyword returns the "default" or "empty" value for a variable of the requested type. For all reference types (defined with class , delegate , etc), this is null . For value types (defined with struct , enum , etc) it's an all-zeroes value (for example, int 0 , DateTime 0001-01-01 00:00:00 , etc).


1 Answers

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.

like image 161
Stephen Cleary Avatar answered Oct 16 '22 16:10

Stephen Cleary