Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use named Tuple with generic type declaration?

I know we can declared a named tuples like:

var name = (first:"Sponge", last:"Bob");

However, I cannot figure out how to combined the named tuple with a generic type, such as Dictionary.

I have tried the below variations, but no luck:

Dictionary<string, (string, string)> name = new Dictionary<string, (string, string)>();

// this assignment yields this message:
// The tuple element name 'value' is ignored because a different name or no name is specified by the 
// target type '(string, string)'.
// The tuple element name 'limitType' is ignored because a different name or no name is specified 
// by the target type '(string, string)'.
name["cast"] = (value:"Sponge", limitType:"Bob");  

// I tried putting the name in front and at the end of the type, but no luck
// Both statements below produce syntactic error:
Dictionary<string, (value:string, string)> name;
Dictionary<string, (string:value, string)> name;

Does anyone know if C# supports the scenario above?

like image 572
Johnny Wu Avatar asked Mar 07 '20 21:03

Johnny Wu


People also ask

Is tuple a generic type?

Having well-defined messages specified in a system is a desirable aspect of an application as it's clear which messages an object or a method can handle. However, in C#7, things get interesting because tuples have been added to the language, and you can use tuple definitions as types to generics!

Are tuples reference types?

Tuple types are reference types. System. ValueTuple types are mutable.

Can tuple be null C#?

Comparing null to null The null == null comparison is allowed, and the null literals do not get any type. In tuple equality, this means, (0, null) == (0, null) is also allowed and the null and tuple literals don't get a type either.

How do you declare a tuple in C#?

The Tuple<T> class was introduced in . NET Framework 4.0. A tuple is a data structure that contains a sequence of elements of different data types. It can be used where you want to have a data structure to hold an object with properties, but you don't want to create a separate type for it.


2 Answers

You have a two options here. First one is to declare the named tuple with custom item names at Dictionary declaration, like that

var name = new Dictionary<string, (string value, string limitType)>();
name["cast"] = ("Sponge", "Bob"); //or name["cast"] = (value: "Sponge", limitType: "Bob");

And access an items in the following way

var value = name["cast"].value;

Second one is to use an unnamed tuple with default item names (Item1, Item2, etc.)

var name = new Dictionary<string, (string, string)>();
name["cast"] = ("Sponge", "Bob");

Language support for tuples was added in C# 7, please ensure that you are using this language version, or install System.ValueTuple package, if something is missing

like image 106
Pavel Anikhouski Avatar answered Oct 12 '22 00:10

Pavel Anikhouski


In this case what matters is the names in the declaration of the Dictionary:

Dictionary<string, (string First, string Last)> SomeDictionary
    = new Dictionary<string, (string, string)>();

Then you can use the names like this:

var value = SomeDictionary["SomeKey"];
var first = value.First;
var last = value.Last;
//var (first, last) = SomeDictionary["SomeKey"]; // alternative, Tuple deconstruction
like image 30
Theodor Zoulias Avatar answered Oct 11 '22 23:10

Theodor Zoulias