Creating a constant Dictionary in C#

前端 未结 10 2052
独厮守ぢ
独厮守ぢ 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:46

    This is the closest thing you can get to a "CONST Dictionary":

    public static int GetValueByName(string name)
    {
        switch (name)
        {
            case "bob": return 1;
            case "billy": return 2;
            default: return -1;
        }
    }
    

    The compiler will be smart enough to build the code as clean as possible.

    0 讨论(0)
  • 2020-12-04 08:47

    I am not sure why no one mentioned this but in C# for things that I cannot assign const, I use static read-only properties.

    Example:

    public static readonly Dictionary<string, string[]> NewDictionary = new Dictionary<string, string[]>()
            {
                { "Reference1", Array1 },
                { "Reference2", Array2 },
                { "Reference3", Array3 },
                { "Reference4", Array4 },
                { "Reference5", Array5 }
            };
    
    0 讨论(0)
  • 2020-12-04 08:48

    Why not use namespaces or classes to nest your values? It may be imperfect, but it is very clean.

    public static class ParentClass
    {
        // here is the "dictionary" class
        public static class FooDictionary
        {
            public const string Key1 = "somevalue";
            public const string Foobar = "fubar";
        }
    }
    

    Now you can access .ParentClass.FooDictionary.Key1, etc.

    0 讨论(0)
  • 2020-12-04 08:50

    There are precious few immutable collections in the current framework. I can think of one relatively pain-free option in .NET 3.5:

    Use Enumerable.ToLookup() - the Lookup<,> class is immutable (but multi-valued on the rhs); you can do this from a Dictionary<,> quite easily:

        Dictionary<string, int> ids = new Dictionary<string, int> {
          {"abc",1}, {"def",2}, {"ghi",3}
        };
        ILookup<string, int> lookup = ids.ToLookup(x => x.Key, x => x.Value);
        int i = lookup["def"].Single();
    
    0 讨论(0)
  • 2020-12-04 08:52

    Why not:

    public class MyClass
    {
        private Dictionary<string, int> _myCollection = new Dictionary<string, int>() { { "A", 1 }, { "B", 2 }, { "C", 3 } };
    
        public IEnumerable<KeyValuePair<string,int>> MyCollection
        {
            get { return _myCollection.AsEnumerable<KeyValuePair<string, int>>(); }
        }
    }
    0 讨论(0)
  • 2020-12-04 08:54

    Creating a truly compile-time generated constant dictionary in C# is not really a straightforward task. Actually, none of the answers here really achieve that.

    There is one solution though which meets your requirements, although not necessarily a nice one; remember that according to the C# specification, switch-case tables are compiled to constant hash jump tables. That is, they are constant dictionaries, not a series of if-else statements. So consider a switch-case statement like this:

    switch (myString)
    {
       case "cat": return 0;
       case "dog": return 1;
       case "elephant": return 3;
    }
    

    This is exactly what you want. And yes, I know, it's ugly.

    0 讨论(0)
提交回复
热议问题