2

I want to initialize a static readonly array of ValueTuples, I'd like to use the approach from this SO answer:

var tupleList = new (int Index, string Name)[]
  {
      (1, "cow"),
      (5, "chickens"),
      (1, "airplane")
  };

But it doesn't work for a static member, I have to declare the type of tupleList. I can do it as a Tuple like this, but I don't know how to do it as a ValueTuple:

static readonly Tuple<uint, string>[] tupleList= new Tuple<uint, string>[]
{
    new Tuple<uint, string>(0x1, "string1"),
    ...
};

But I'd prefer to use the cleaner format if I can figure out the right type, so far I've tried a variety of types with no luck.

Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • 4
    I am having no issue with the following: `static readonly (int id, string name)[] Tuples = new [] { (1, "hello"), (2, "world") };`. What code have you tried that is causing the issue, and what is the error the compiler gives you? – Jonathon Chase Feb 27 '19 at 19:39
  • This is not unique to tuples, you can't use `var` to declare class members, which means you can't use anonymous types either. – D Stanley Feb 27 '19 at 19:39
  • Personal preference, but _every_ time I think of using a Tuple, I tell myself there'd be more clarity with a class, and I opt for creating a class. Even if it's just a POCO. What's the benefit of a tuple, in your scenario? – ps2goat Feb 27 '19 at 19:41
  • 4
    Note that that's not a different syntax for the same code. The first snippet is using `ValueTuple` instead of `Tuple`, which impacts the behavior. – Servy Feb 27 '19 at 19:49
  • Thanks for the tips, I actually did try the correct approach so now I'm thinking that perhaps this tool does not support this feature properly. The benefit of a tuple is to keep the code shorter. It's only being used in this one module so a class is not particularly useful. – Alan Moore Feb 27 '19 at 22:23
  • I'm not sure why this is marked as a duplicate, the question is not why a class field can't be marked as var, please read the question carefully! – Alan Moore Feb 27 '19 at 22:27

1 Answers1

5

You can't use the "cleaner" var (implicit, but still strongly typed) type but you can initialize a tuple as the commenters have suggested. The "cleanest" you can get away with is this, which uses type inference on the array:

(int Index, string Name)[] tupleList = {
    (1, "cow"),
    (5, "chickens"),
    (1, "airplane")
};

That, at least, allows you to avoid specifying the type twice, which is one reason people use var.

The language does not support the use of var when declaring member variables. This is true of both static and instance fields.

There's a historic reason for this. That's not to say it couldn't change if you propose a change and it's vetted by the community.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • Does this tuple spec only work with var then? – Alan Moore Feb 27 '19 at 19:54
  • @AlanMoore only [*local* variables can be implicitly typed](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables). Class members must be explicitly typed. – p.s.w.g Feb 27 '19 at 19:58
  • @AlanMoore It has nothing to do with tuples. – ckuri Feb 27 '19 at 19:59
  • 1
    Although the answer is correct, the `You can't` part is wrong because the question was not whether or not it's possible to use `var`. @AlanMoore All you need to do is replace `var` with `(int id, string name)[]` in your first code block and you're good to go. Check [Jonathon's comment](https://stackoverflow.com/users/5402620/jonathon-chase) above. – 41686d6564 stands w. Palestine Feb 27 '19 at 20:01
  • I'll make that more clear. – Kit Feb 27 '19 at 20:02
  • Thanks @AhmedAbdelhameed! That is correct, it appears that many have misread my question. – Alan Moore Feb 27 '19 at 22:54