0

this question is not a duplicate of Better naming in Tuple classes than "Item1", "Item2"

In the linked Question, they ask about assigning names to tuple elements. I am asking about naming the entire type which is a tuple with named elements.

I have a tuple with named items in my code:

(string kind, string colour, string length) a = ("annual", "blue", "short);
var myKind = a.kind;
var myColour = a.blue;
var myLength = a.short;

I would like this type to be named so I can use it like this (similar to C++ typedef):

FlowerInformation a = ("annual", "blue", "short);
var myKind = a.kind;
var myColour = a.colour;
var myLength = a.length;

I could use the "using" directive, like so:

using FlowerInformation = System.ValueTuple<string , string , string>;

This way the type has a name, but the items are not named, so my code must become this:

FlowerInformation a = ("annual", "blue", "short);
var myKind = a.Item1;
var myColour = a.Item2;
var myLength = a.Item3;

What I'd really like is a named tuple type with named members. Is it possible in C#?

The following doesn't work:

using FlowerInformation = (string kind, string colour, string length);
Natalia Zoń
  • 980
  • 2
  • 12
  • 22
  • FYI you didn't seem to have correctly understood what I am asking about. I am NOT asking about how to create a tuple with named items. I am asking about how to name the type of a tuple with named items. – Natalia Zoń Feb 22 '23 at 17:16
  • 3
    This is not how Tuples work. If you want something with named items to have a name make a Class. What you're describing is a Class. – Logarr Feb 22 '23 at 17:19
  • I obviously know I can use a Class instead, but I wanted to know if a tuple can have a named type. I assume not, in this case? – Natalia Zoń Feb 22 '23 at 17:21
  • 4
    No, you can't rename a type in c# – Rufus L Feb 22 '23 at 17:24
  • 2
    No, it cannot. Tuples are shortcuts to avoid having to create a whole Class/Struct for a one off, multi-item object. The Value Tuple system introduced in later C# versions, and the ability to name the items, were both ways to give us more incentive to use Tuples instead of one off classes. Let me ask you this, what problem do you have that giving the Tuple a named Type would solve? And why is a Class not a solution to that problem? – Logarr Feb 22 '23 at 17:24
  • A Class is a possible solution to that problem. I'm learning C# and was curious if there is a way to name a tuple, that's all. – Natalia Zoń Feb 22 '23 at 17:25
  • `class FlowerInformation { public string Kind; public string Colour; public string Length }` – Rufus L Feb 22 '23 at 17:26
  • 2
    You're welcome! Glad it helped. That along with my *"No, youy can't rename a type in c#"* comment should be the answer you're looking for – Rufus L Feb 22 '23 at 17:31
  • Actually, you are wrong there, since you can do this: "using MyOwnType = System.ValueTuple;", Just not with a tuple of named elements. So you *can* rename a type (regardless of whether you should or not), just not a tuple with named elements. – Natalia Zoń Feb 22 '23 at 17:49
  • 2
    That's called Type Aliasing. https://stackoverflow.com/questions/7082238/c-how-to-create-aliases-for-classes Here's a better, more exact duplicate of your question https://stackoverflow.com/questions/43622918/can-you-name-a-tuple-signature-in-c-sharp-7 – gunr2171 Feb 22 '23 at 17:56
  • Thanks @gunr2171 that's actually useful! I didn't know they were called aliases. And yeah indeed that one is a duplicate, so I guess my question can be marked as such. – Natalia Zoń Feb 22 '23 at 17:57

1 Answers1

2

Consider using the new record type introduced in C# 9. It is not a Tuple, but if you want a custom type with named fields, it is a viable alternative.

// Declaration
record FlowerInformation(string kind, string colour, string length);

// Construction
FlowerInformation a = new("annual", "blue", "short");

// Accessing a field
var myKind = a.kind;
var myColour = a.colour;
var myLength = a.length;

// Deconstruction
var (kind, colour, length) = a;

Use record or record class to declare a reference type (similar to Tuple), and record struct to declare a value type (similar to ValueTuple and () syntax).

nevermind
  • 2,300
  • 1
  • 20
  • 36