0

Is there a smarter way to define a dictionary like this:

class field definition:

public Dictionary<string, object> paramList { get; set; }

access later:

dt.paramList = new Dictionary<string, object> { { "id", rt_id }, { "gen", rt_gen }, { "rtext", rchar } };

dt.paramList = { { "id", rt_id }, { "gen", rt_gen }, { "rtext", rchar } };

... second term works obv. not, despite an array could be initialized by:

int[,] b = { { 0,1}, { 1,1} };
frameworker
  • 298
  • 1
  • 3
  • 16
  • If you are looking for shorter syntax, there is not . however you could `array.ToDictionary(x => x[0], x => x[1])` though total printable characters saved = 0 – TheGeneral Apr 09 '19 at 09:20
  • You can only directly initialize a dictionary or array when you construct it, there is no simplified syntax for "creating a dictionary", but there is one for "initializing one that is being constructed". – Lasse V. Karlsen Apr 09 '19 at 09:20
  • So you can use `new Dictionary<...> { ... }`, but there is no simple syntax with just the braces, you need to construct the dictionary as well. – Lasse V. Karlsen Apr 09 '19 at 09:21
  • What do you mean when you say "smartest"? – Marco Salerno Apr 09 '19 at 09:21
  • I haven't see syntax restriction since the time we redirect our diplay to a printer so we don't eat memory. The best syntax is alway the most readable the well indented one that doesnt require me to scroll to the right – xdtTransform Apr 09 '19 at 09:21
  • If you really need some syntaxe spice. You may use a funct that return a dict taking a multi dim as param. The total gain will be a face kick in most review. especially if you create an wrapper around a type (here `Dictionary`) because you feeled like 1 char in a row was to mutch. – xdtTransform Apr 09 '19 at 09:23
  • Possible duplicate of [Proper way to initialize a C# dictionary with values?](https://stackoverflow.com/questions/17047602/proper-way-to-initialize-a-c-sharp-dictionary-with-values) – xdtTransform Apr 09 '19 at 09:26
  • If a new way that satisfy your requirement exist without trick. It will be welcom on the dupe target, in the listing of all possible way to initialise a `Dictionary`. – xdtTransform Apr 09 '19 at 09:28
  • [This](https://stackoverflow.com/questions/23565974/how-to-add-multiple-values-to-dictionary-in-c-sharp) may be worth reading. As Something like myDict.AddRange( ..) would have been a more consise syntaxt depending on how you named the addrange extention method. – xdtTransform Apr 09 '19 at 09:34
  • An other way arround is to initalise an array and linQ it to a dictionary. – xdtTransform Apr 09 '19 at 09:37

1 Answers1

0

The meaning of the line dt.paramList = { { "id", rt_id }, { "gen", rt_gen }, { "rtext", rchar } }; is not unambiguous. It could mean that we should try to assign a two dimensional array to the Dictionary<string, object> property. If the compiler was "smart" and interpreted this as a Dictionary, when there is another possible meaning of the statement, the compiler would so to say become coauthor of your code. This could introduce unintended errors :)

The syntax of Arrray initialization in C# is a special case that is (somewhat) inherited from C. Making assumptions of more complex objects is harder.

And, neither array initialization would allow:

public int[,] paramList { get; set; }
...
paramList  = { { 0,1}, { 1,1} };

You'd have to write:

x.paramList  = new [,]{ { 0,1}, { 1,1} };
mortb
  • 9,361
  • 3
  • 26
  • 44
  • 1
    The class field definition says dt.paramList is a dictionary, not an array. So the assigmment should be clear for the compiler_? – frameworker Apr 09 '19 at 12:13
  • I'm pointing out that the code may mean different things. It is risky for the compiler to choose one "correct" interpretation. When something is ambiguous it is better to not let it compile. And when the definition `public Dictionary paramList { get; set; }` is potentially situated somewhere completely different in the code base than the line `paramList = { { 0,1}, { 1,1} };`. It would not be fully instantly unambiguously understandable for a human without having to find the definition. I think the syntax might lead to head scratching.... :) – mortb Apr 09 '19 at 13:01
  • You can try to put an issue in the csharp github project and see if the developers of the language see it differently https://github.com/dotnet/csharplang – mortb Apr 09 '19 at 13:06