using `is` operator with value type tuples gives errorC# 7 Pattern Match with a tupleHow do you give a C# Auto-Property a default value?Direct casting vs 'as' operator?Collection was modified; enumeration operation may not executeGet int value from enum in C#How to loop through all enum values in C#?Type Checking: typeof, GetType, or is?Extension methods defined on value types cannot be used to create delegates - Why not?LINQ .Cast() extension method fails but (type)object worksHow to invoke a delegate with a null parameter?How do I populate current class with new class values?

On studying Computer Science vs. Software Engineering to become a proficient coder

How do employ ' ("prime") in math mode at the correct depth?

As programers say: Strive to be lazy

What kind of SATA connector is this?

What are the components of a legend (in the sense of a tale, not a figure legend)?

travelling home

Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?

Loading Latex packages into Mathematica

Does gravity affect the time evolution of a QM wave function?

What to do if SUS scores contradict qualitative feedback?

Help in identifying a mystery wall socket

Conditional probability - sum of dice is even given that at least one is a five

what does a native speaker say when he wanted to leave his work?

Longest Text in Latin

Could there be a material that inverts the colours seen through it?

Earliest use of "rookie"?

Was this character’s old age look CGI or make-up?

Quote from Leibniz

Ito`s Lemma problem

What is the largest number of identical satellites launched together?

What does 'pisketti' mean here?

Jumping frame contents with beamer and pgfplots

What information do scammers need to withdraw money from an account?

Why was Thor doubtful about his worthiness to Mjolnir?



using `is` operator with value type tuples gives error


C# 7 Pattern Match with a tupleHow do you give a C# Auto-Property a default value?Direct casting vs 'as' operator?Collection was modified; enumeration operation may not executeGet int value from enum in C#How to loop through all enum values in C#?Type Checking: typeof, GetType, or is?Extension methods defined on value types cannot be used to create delegates - Why not?LINQ .Cast() extension method fails but (type)object worksHow to invoke a delegate with a null parameter?How do I populate current class with new class values?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















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 exception
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 an suitable to use for my case but I can't use it due to giving error.










share|improve this question
























  • AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

    – Slai
    1 hour ago












  • @Slai I also tried it, but the same error appears.

    – ilkerkaran
    1 hour ago






  • 3





    if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

    – György Kőszeg
    1 hour ago


















8















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 exception
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 an suitable to use for my case but I can't use it due to giving error.










share|improve this question
























  • AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

    – Slai
    1 hour ago












  • @Slai I also tried it, but the same error appears.

    – ilkerkaran
    1 hour ago






  • 3





    if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

    – György Kőszeg
    1 hour ago














8












8








8








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 exception
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 an suitable to use for my case but I can't use it due to giving error.










share|improve this question
















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 exception
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 an suitable to use for my case but I can't use it due to giving error.







c# valuetuple






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









GSerg

61.6k15111241




61.6k15111241










asked 1 hour ago









ilkerkaranilkerkaran

1,2691224




1,2691224












  • AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

    – Slai
    1 hour ago












  • @Slai I also tried it, but the same error appears.

    – ilkerkaran
    1 hour ago






  • 3





    if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

    – György Kőszeg
    1 hour ago


















  • AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

    – Slai
    1 hour ago












  • @Slai I also tried it, but the same error appears.

    – ilkerkaran
    1 hour ago






  • 3





    if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

    – György Kőszeg
    1 hour ago

















AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

– Slai
1 hour ago






AFAIK value tuples don't store item name information, so there is probably no way to check if the items are id and name. I am guessing it might work without names if (val is (int, int) pair)

– Slai
1 hour ago














@Slai I also tried it, but the same error appears.

– ilkerkaran
1 hour ago





@Slai I also tried it, but the same error appears.

– ilkerkaran
1 hour ago




3




3





if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

– György Kőszeg
1 hour ago






if (val is ValueTuple<int,string> pair) works though. Somewhat related: stackoverflow.com/questions/44706498/…

– György Kőszeg
1 hour ago













1 Answer
1






active

oldest

votes


















6














You can write this:



if (val is (int id, int name))

Console.WriteLine($"id: id; name: name");



However this boxes id and name, which is surprising.



Edit Looks like this might be a new thing in C# 8. See SharpLab. Below C# 8, your only options are probably the ones below.



Or this:



if (val is ValueTuple<int, int> pair)

Console.WriteLine($"id: pair.Item1; name: pair.Item2");



It looks like you can also deconstruct the ValueTuple inline:



if (val is ValueTuple<int, int> (var id, var name))

Console.WriteLine($"id: id; name: name");



... which is somewhat horrific, but appears to be legal.



You can of course make it a bit clearer:



if (val is ValueTuple<int, int> pair)

var (id, name) = pair;
Console.WriteLine($"id: id; name: name");



I might have expected val is (int, int) pair) to work, but it seems nobody's designed this case (yet).






share|improve this answer




















  • 1





    The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

    – ilkerkaran
    1 hour ago











  • @ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

    – canton7
    1 hour ago











  • @ilkerkaran found another way, added to my answer

    – canton7
    1 hour ago











  • I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

    – ilkerkaran
    1 hour ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56103160%2fusing-is-operator-with-value-type-tuples-gives-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














You can write this:



if (val is (int id, int name))

Console.WriteLine($"id: id; name: name");



However this boxes id and name, which is surprising.



Edit Looks like this might be a new thing in C# 8. See SharpLab. Below C# 8, your only options are probably the ones below.



Or this:



if (val is ValueTuple<int, int> pair)

Console.WriteLine($"id: pair.Item1; name: pair.Item2");



It looks like you can also deconstruct the ValueTuple inline:



if (val is ValueTuple<int, int> (var id, var name))

Console.WriteLine($"id: id; name: name");



... which is somewhat horrific, but appears to be legal.



You can of course make it a bit clearer:



if (val is ValueTuple<int, int> pair)

var (id, name) = pair;
Console.WriteLine($"id: id; name: name");



I might have expected val is (int, int) pair) to work, but it seems nobody's designed this case (yet).






share|improve this answer




















  • 1





    The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

    – ilkerkaran
    1 hour ago











  • @ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

    – canton7
    1 hour ago











  • @ilkerkaran found another way, added to my answer

    – canton7
    1 hour ago











  • I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

    – ilkerkaran
    1 hour ago















6














You can write this:



if (val is (int id, int name))

Console.WriteLine($"id: id; name: name");



However this boxes id and name, which is surprising.



Edit Looks like this might be a new thing in C# 8. See SharpLab. Below C# 8, your only options are probably the ones below.



Or this:



if (val is ValueTuple<int, int> pair)

Console.WriteLine($"id: pair.Item1; name: pair.Item2");



It looks like you can also deconstruct the ValueTuple inline:



if (val is ValueTuple<int, int> (var id, var name))

Console.WriteLine($"id: id; name: name");



... which is somewhat horrific, but appears to be legal.



You can of course make it a bit clearer:



if (val is ValueTuple<int, int> pair)

var (id, name) = pair;
Console.WriteLine($"id: id; name: name");



I might have expected val is (int, int) pair) to work, but it seems nobody's designed this case (yet).






share|improve this answer




















  • 1





    The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

    – ilkerkaran
    1 hour ago











  • @ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

    – canton7
    1 hour ago











  • @ilkerkaran found another way, added to my answer

    – canton7
    1 hour ago











  • I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

    – ilkerkaran
    1 hour ago













6












6








6







You can write this:



if (val is (int id, int name))

Console.WriteLine($"id: id; name: name");



However this boxes id and name, which is surprising.



Edit Looks like this might be a new thing in C# 8. See SharpLab. Below C# 8, your only options are probably the ones below.



Or this:



if (val is ValueTuple<int, int> pair)

Console.WriteLine($"id: pair.Item1; name: pair.Item2");



It looks like you can also deconstruct the ValueTuple inline:



if (val is ValueTuple<int, int> (var id, var name))

Console.WriteLine($"id: id; name: name");



... which is somewhat horrific, but appears to be legal.



You can of course make it a bit clearer:



if (val is ValueTuple<int, int> pair)

var (id, name) = pair;
Console.WriteLine($"id: id; name: name");



I might have expected val is (int, int) pair) to work, but it seems nobody's designed this case (yet).






share|improve this answer















You can write this:



if (val is (int id, int name))

Console.WriteLine($"id: id; name: name");



However this boxes id and name, which is surprising.



Edit Looks like this might be a new thing in C# 8. See SharpLab. Below C# 8, your only options are probably the ones below.



Or this:



if (val is ValueTuple<int, int> pair)

Console.WriteLine($"id: pair.Item1; name: pair.Item2");



It looks like you can also deconstruct the ValueTuple inline:



if (val is ValueTuple<int, int> (var id, var name))

Console.WriteLine($"id: id; name: name");



... which is somewhat horrific, but appears to be legal.



You can of course make it a bit clearer:



if (val is ValueTuple<int, int> pair)

var (id, name) = pair;
Console.WriteLine($"id: id; name: name");



I might have expected val is (int, int) pair) to work, but it seems nobody's designed this case (yet).







share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









canton7canton7

6,4931829




6,4931829







  • 1





    The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

    – ilkerkaran
    1 hour ago











  • @ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

    – canton7
    1 hour ago











  • @ilkerkaran found another way, added to my answer

    – canton7
    1 hour ago











  • I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

    – ilkerkaran
    1 hour ago












  • 1





    The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

    – ilkerkaran
    1 hour ago











  • @ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

    – canton7
    1 hour ago











  • @ilkerkaran found another way, added to my answer

    – canton7
    1 hour ago











  • I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

    – ilkerkaran
    1 hour ago







1




1





The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

– ilkerkaran
1 hour ago





The first code block gives the same error. The second one works but I don't want to use Item1, Item2 etc.

– ilkerkaran
1 hour ago













@ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

– canton7
1 hour ago





@ilkerkaran It works in sharplab - might be new in C# 8. sharplab.io/…

– canton7
1 hour ago













@ilkerkaran found another way, added to my answer

– canton7
1 hour ago





@ilkerkaran found another way, added to my answer

– canton7
1 hour ago













I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

– ilkerkaran
1 hour ago





I think my editor does not let me to use c#8 yet (I think because it is preview). Your updated answer is probably the closest I can get for now.

– ilkerkaran
1 hour ago



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56103160%2fusing-is-operator-with-value-type-tuples-gives-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Siegen Nawigatsjuun

Log på Navigationsmenu

Log på Navigationsmenu