I'm using Visual Studion 2017 version 15.5.2, and C# version 7.2. To the point:
Color c = default; // or: c = default(Color); no difference
Debug.Print($"{c.Equals(default(Color))}"); // true
Debug.Print($"{c.Equals(default)}"); // false WHY?!
But if I use ValueTuple:
(string s, int i) t = default;
Debug.Print($"{t.Equals(default((string, int)))}"); // true
Debug.Print($"{t.Equals(default)}"); // true
Is it supposed to be like this?
Is this Windows Forms?
Because in WinForms, System.Drawing.Color.Equals()
doesn't have an overload that takes a Color
. Instead, it only has the one from Object
. In WPF, System.Windows.Media.Color.Equals()
contains an overload that takes a Color
.
When default
is passed as an argument to Color.Equals(Object)
, what gets passed is default(Object)
since the compiler infers Object
to be the type based on its signature. From the docs:
The
default
literal produces the same value as the equivalentdefault(T)
whereT
is the inferred type.
Clearly, default(Color)
isn't equivalent to default(Object)
, since Color
is a value type and Object
is a reference type (which defaults to null).
ValueTuple.Equals()
, on the other hand, takes another ValueTuple
, so the compiler has no trouble inferring the type of default
as default(ValueTuple)
.
Edit:
As of .NET Core 2.0, System.Drawing.Color.Equals()
does have an overload that takes a Color
. The compiler would have no trouble inferring the type of default as default(Color); therefore, it would now return true.
@fharreau is correct: System.Drawing.Color
does not implement an Equals(Color)
method, so $"{t.Equals(default)}"
binds to the only method available: Equals(Object)
. Thus, default
resolves to default(Object)
or null
.
If you use System.Windows.Media.Color
from WPF, which does implement Equals(Color)
, then you'll see the expected results:
System.Windows.Media.Color c = default;
Console.WriteLine($"{c.Equals(default(System.Windows.Media.Color))}"); // true
Console.WriteLine($"{c.Equals(default)}"); // true
ValueTuple
also provides an Equals
to compare against another tuple, which is why you saw the expected result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With