Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value tuple is default

How to check if a System.ValueTuple is default? Rough example:

(string foo, string bar) MyMethod() => default;  // Later var result = MyMethod(); if (result is default){ } // doesnt work 

I can return a default value in MyMethod using default syntax of C# 7.2. I cannot check for default case back? These are what I tried:

result is default result == default result is default(string, string) result == default(string, string) 
like image 300
nawfal Avatar asked Apr 30 '18 12:04

nawfal


1 Answers

If you really want to keep it returning default, you could use

result.Equals(default) 

the built-in Equals method of a ValueTuple should work.

As of C# 7.3 value tuples now also support comparisons via == and != fully, Meaning you can now also do

result == default and it should work the same.

like image 148
Gibbon Avatar answered Sep 23 '22 12:09

Gibbon