I am trying to check if an object
variable is (int, int)
and if so I will use the casted variable so I have tried the codes below:
//this one gives the error
public void MyMethodWithIs(object val)
{
if(val is (int id, int name) pair)
{
ConsoleWriteLine($"{pair.id}, {pair.name}");
}
}
//This one works
public void MyMethodWithAs(object val)
{
var pair = val as (int id, int name)?;
if(pair!=null)
{
ConsoleWriteLine($"{pair.id}, {pair.name}");
}
}
The MyMethodWithIs
method gives the error below in the editor:
No suitable deconstruct instance or extension method was found for type
My Question
Why one works fine but the other gives an error at all? I think MyMethodWithIs
more readable and suitable to use for my case but I can't use it due to giving an error.
if (val is (int, int) pair)
– Coanif (val is ValueTuple<int,string> pair)
works though. Somewhat related: #44706998 – Sikata