Parenthesis Tuple(?) Syntax in C#

Advertisements

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.:

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.

Leave a Reply Cancel reply