check against: null vs default()?
Asked Answered
L

7

35

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?

Lice answered 12/7, 2010 at 15:50 Comment(1)
Use default(T), it's more readable.Hyperaesthesia
C
44

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.

Cleaning answered 12/7, 2010 at 15:52 Comment(5)
Well, I beg to differ: the first comparison is typesafe (or at least should be) this is what is basically wrong with null in c#, null == null is true but the left null could be a string and the right null could be a complex object so default(string) should not be equal to default(complex object) it would be good if everyone stopped using null. blog.miguelbernard.com/why-null-in-csharp-is-so-badBibelot
@Null: You're assuming that null values have a type. Is that really true?Cleaning
null has all types and therefore none. Did you read the blog I was referring to ?Bibelot
@Null: Yes, I read it, but I disagree with the idea that assignment implies inheritance. I do agree with the conclusion (even though I disagree with the arguments): enabling nullable type checking is ideal.Cleaning
On that, I do agree too. I'm sorry that I could not come up with better arguments.Bibelot
G
20

Now that we don't need to pass the type to default anymore, default is preferred.

  • It is just as readable

  • It can be used both for value and reference types

  • It can be used in generics

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

Also, after calling

obj = enumerable.FirstOrDefault();

it makes more sense to test for default after that and not for null. Otherwise it should have been FirstOrNull, but value dont have a null value but do have a default.

Grays answered 23/2, 2021 at 7:36 Comment(0)
L
14

There is no difference, but second one is more readable. The best place to use default is when you deal with generics. Common code is return default(T);

Lamson answered 12/7, 2010 at 15:57 Comment(4)
For DateTime it returns "0001-01-01T00:00:00" which is a Valid value... unlike null which is Invalid value.Kob
@YoushaAleayoub the question asks about reference types and DateTime is not a reference type.Lamson
I think that's a matter of opinion. I use default (without parentheses) all over the place because I instinctively know what the default is for the types that I work with. It can mean null or DateTime.Min or 0 for my int. It would be more readable for people that don't automatically register the default value in their head. It can also, for example, help you do a search for all values that are assigned a default value. Instead of searching for null, 0, or Decimal.Zero, you just search for default, == default , or even is defaultEberto
@SaturnK it is a matter of taste and I prefer the explicit values over default keyword (python's zen: explicit is better than implicit). default keyword was introduced to be used with template types, because you can't have an explicit value there when it comes to default.Lamson
W
6

Not different but I think

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

is clearer.

Wolves answered 12/7, 2010 at 15:56 Comment(0)
N
3

My understanding is they are not different. It only matters when you are dealing with value types.

Nessi answered 12/7, 2010 at 15:53 Comment(0)
P
3

I would definitely go with the specific check against null. Because if the type of the _settings class ever changes you may run into reference issues. At minimum it would require a change to the code breaking the open/close policy.

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

This IMO is safer and cleaner.

Plier answered 12/7, 2010 at 16:0 Comment(0)
S
2

As has been mentioned, there is no difference... but you might want to use default(<type>) anyway, to handle the cases where it's not a reference type. Typically this is only in generics, but it's a good habit to form for the general case.

Stele answered 12/7, 2010 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.