Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name ValueTuple properties when creating with new

Tags:

c#

.net

c#-7.0

I know I can name parameters when I create a tuple implicitly like:

var me = (age: 21, favoriteFood: "Custard");

Is it possible to name parameters when a tuple is created explicitly? i.e.

var me = new ValueTuple<int, string>(21, "Custard");
like image 200
BanksySan Avatar asked Apr 22 '17 23:04

BanksySan


People also ask

How do you create a ValueTuple?

You can create a pair value tuple by using ValueTuple <T1, T2>(T1, T2) constructor. It initializes a new instance of the ValueTuple <T1, T2> struct. But when you create a value tuple using this constructor, then you have to specify the type of the element stored in the value tuple.

What is a ValueTuple?

ValueTuple is a structure introduced in C# 7.0 which represents the value type Tuple. It is already included in . NET Framework 4.7 or higher version. It allows you to store a data set which contains multiple values that may or may not be related to each other.

Is tuple value type?

Tuple types are value types; tuple elements are public fields. That makes tuples mutable value types.

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.


1 Answers

No, you can't. The ValueTuple types are actually independent of the named field support in C#. The latter works more like named properties for anonymous types. That is, the compiler analyzes the code and generates aliases to the appropriate members according to your declarations and usages. It is through the assignment that the compiler learns the names of the fields. Since the basic constructor syntax doesn't provide a mechanism to name the fields, you can't use that to directly generate a tuple with named fields.

Of course, there are ways you can re-interpret the value returned from the constructor syntax, to assign names to that returned value. I'm assuming you're aware of that approach and are looking for something more direct.

As an example of what I mean by "re-interpret", you could do something like this:

static (int value, string text) ConvertToNamed((int, string) t) => t;

then this would name the fields, in a new variable:

var t1 = new ValueTuple<int, string>(21, "hello");
var t2 = ConvertToNamed(t1);

The variable t1 is stuck with Item1 and Item2. But the compiler will implicitly generate the desired names for the variable t2.

Maybe a better example is one where you don't require the additional method:

(int value, string text) t = new ValueTuple<int, string>(21, "hello");

Again, you're not really naming the fields in the constructor syntax, but they are reinterpreted by the local variable declaration.

This is probably not a serious limitation. In a scenario where there's a desire to have a persistent, easily-assigned name, it's probably better to declare a user-defined type than to use the tuple syntax anyway. You can write deconstructors for user-defined types as well, and declaring types like that means the names are first-class citizens when it comes to reflection, dynamic, etc.

like image 140
Peter Duniho Avatar answered Sep 25 '22 13:09

Peter Duniho