问题
From Microsoft new-features-in-c-7-0:
public void PrintStars(object o)
{
if (o is null) return; // constant pattern "null"
if (!(o is int i)) return; // type pattern "int i"
WriteLine(new string('*', i));
}
Whats the diferrence of o == null and o is null?
回答1:
The o is null is translated to object.Equals(null, o) (you can see it here).
The object.Equals code is written as:
public static bool Equals(Object objA, Object objB)
{
if (objA == objB)
{
return true;
}
if (objA == null || objB == null)
{
return false;
}
return objA.Equals(objB);
}
so in the end there will be a o == null (the first if). Note that System.Object doesn't define the operator==, so the one used is the one for reference types that is reference equality.
Theorically, by watching the called code, one could think that o == null (with o a System.Object) should be faster than o is null (less operations)... But who knows? :-)
The end result is that, through two different routes, o is null and o == null (with o a System.Object) return the same result.
By looking we can even see that o == null is the same as object.ReferenceEquals(o, null) (with o a System.Object) :-).
the interesting question should be, why doesn't the C# compiler translates the x is null to object.ReferenceEquals(x, null)?. Note that, thanks to how the boxing of nullable types is done, it would work even for:
int? a = null;
if (a is null) { /* */ }
changes to the compiler made this response invalid... If you click on the "here" link you can see it
回答2:
The differences between "is" and == is "is" special that it act as == if you compare to value and act as typeof(type) when you compare type of object to type.
来源:https://stackoverflow.com/questions/42814245/pattern-matching-equal-null-vs-is-null