One difference between != null
and is { }
is that the first can be overridden whereas the second cannot.
For example, if you paste the following into a new Console app and run it
DoNotDoThis? a = new(); // a isn't null
Console.WriteLine("a == null: {0}", a == null);
Console.WriteLine("a != null: {0}", a != null);
Console.WriteLine();
Console.WriteLine("a is null: {0}", a is null);
Console.WriteLine("a is not null: {0}", a is not null);
public class DoNotDoThis
{
public static bool operator ==(DoNotDoThis? a, DoNotDoThis? b)
{
return ReferenceEquals(b, null);
}
public static bool operator !=(DoNotDoThis? a, DoNotDoThis? b)
{
return !(a == b);
}
}
you get the following output
a == null: True
a != null: False
a is null: False
a is not null: True
Interestingly, ReSharper gives you a warning on the first line:
Expression is always false
requestHeaders
is anonymous object and has any value except null, actually you are right withnull
checks – Gahl