Creating a constant Dictionary in C#

前端 未结 10 2102
独厮守ぢ
独厮守ぢ 2020-12-04 08:21

What is the most efficient way to create a constant (never changes at runtime) mapping of strings to ints?

I\'ve tried us

10条回答
  •  余生分开走
    2020-12-04 08:56

    There does not seem to be any standard immutable interface for dictionaries, so creating a wrapper seems like the only reasonable option, unfortunately.

    Edit: Marc Gravell found the ILookup that I missed - that will allow you to at least avoid creating a new wrapper, although you still need to transform the Dictionary with .ToLookup().

    If this is a need constrained to a specific scenario, you might be better off with a more business-logic-oriented interface:

    interface IActiveUserCountProvider
    {
        int GetMaxForServer(string serverName);
    }
    

提交回复
热议问题