Noticed a dearth of code and docs in answers, so I tried out a few different equality checks in .NET 5, all with ==
, to see how they'd evaluate. Elucidating.
The bottom line seems to be if all individual values in the tuple are their own versions of default
, the tuple is default
. And if you new up a tuple to = default
everything is set to its own version of default
.
That just kind of makes sense.
Actual code1
NOTE: This test was performed in a .NET 5 console app.
static void TupleDefault()
{
(int first, string second) spam = new(1, "hello");
(int first, string second) spam2 = default;
Console.WriteLine(spam == default); // False
Console.WriteLine(spam2 == default); // True
Console.WriteLine(spam2.first); // 0
Console.WriteLine(spam2.first == 0); // True
Console.WriteLine(spam2.second); // Nothing.
Console.WriteLine(spam2.second == null); // True
// ==== Let's try to create a default "by hand" ====
(int first, string second) spam3 = new(0, null);
// It works!
Console.WriteLine(spam3 == default); // True
}
is
is meant for casting or pattern matching. In this case you are asking for equality – Waitresult == default((string, string))
(edit: extra parentheses are needed) and perhaps evenresult == default
should work, but we're not there yet. – Cellistis
, it does work with compile time constants for equality check as far as I know, includingdefault(T)
. – Emee