Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parenthesis Tuple(?) Syntax in C#

I just saw the following syntax in code: (int, string) testTwo

It seems to mimic a Tuple, but the return types are incompatible with a Tuple. Example:

Tuple<int, string> test = Test.Get(); // This doesn't work
(int, string) testTwo = Test.Get(); // This works

public static class Test
{
  public static (int, string) Get()
  {
    return (1, "2");
  }
}

Seems like you can name the params too, but this appears to be for readability only, e.g.:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public static (int foo, string bar) Get()

  1. What is this syntax called?
  2. What’s the real world use case?

>Solution :

When creating a Tuple in parentheses it’s a value type, specifically it’s a System.ValueTuple. System.Tuple is a reference type.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading