user3100148
user3100148

Reputation: 65

Reading configs into dictionary with a guaranteed order

I have a few configs inside my Appsettings.json that I would like to read into a dictionary which is fairly easy with new framework. This is what my code looks like:

Options File:

public Dictionary<string, string> Mappings { get; set; }

Configs:

Mappings: {
  "a":"b",
  "c":"d",
}

Now I can bind this particular section inside the startup.cs and it's done.

Due to some requirements I have to change the above dictionary to something like this:

public Dictionary<string, IList<string>> Mappings { get; set; }

and new Configs looks like this:

Mappings: {
  "a":["b1","b2],
  "c":["d"],
}

I can read them into dictionary like before but I would like to know if the order of the list is guaranteed to be the one I specified or it can be random. So in above case, for Ilist of key == a, will b1 be always before b2?

I tried on my local and it works, but is this something which asp.net core guarantee?

Upvotes: 0

Views: 224

Answers (1)

Servy
Servy

Reputation: 203836

This will depend entirely on what IList<T> implementation you choose. If you use a List<T>, the items will be in the order added (unless you insert them into a specific position). But an arbitrary IList<T> internally can do whatever it wants to do to the order of the items, as long as it implements the methods provided.

Upvotes: 1

Related Questions