-1

I though I knew all the tricks on the map but recently I saw an assignment where the following is listed as a part of unit test. I've looked at SO and DotNetPearls but couldn't see anything that would catch my eye.

var dictionary = new Dictionary
{
  [a] = a,
  [b] = b,
  [c] = c
};

What kind of syntax is it? Where can I find more info about it?

The syntax is from a code that's a test for job application and I'm not allowed to alter it (although I've already found other irregularities there). The question of the test isn't this particular syntax, of course, it's just a test method used to verify that the actual test satisfies the criteria (but on my VS 17 it doesn't even compile).

  • 1
    @Sujith That's **sooo** not okay. Implementing *Address* and *Person* based on the used constructors is one thing. But using a commonly spread name colliding with a custom class is just evil... –  Jul 05 '17 at 08:33
  • @thomaslevesque I'm not entirely sure if I'm simply missing the point but in the linked question I see only references to *Dictionary* and my question is about *Dictionary*. It's presented that way in the job ad's test so I assumed it was something different (since it doesn't compile on my computer). –  Jul 07 '17 at 14:18
  • There's no such thing as a non-generic `Dictionary` class, there's only a generic version (at least not in the .NET Framework). I think it's safe to assume that the code in your test is incorrect. – Thomas Levesque Jul 07 '17 at 17:33

1 Answers1

1

This is the index initializer syntax, introduced in C# 6. It's equivalent to this:

var dictionary = new Dictionary();
dictionary[a] = a;
dictionary[b] = b;
dictionary[c] = c;

but on my VS 17 it doesn't even compile

Probably because Dictionary<TKey, TValue> is a generic class, and in this code the generic type arguments are not specified.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758