Creating a constant Dictionary in C#

前端 未结 10 2053
独厮守ぢ
独厮守ぢ 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);
    }
    
    0 讨论(0)
  • 2020-12-04 09:01

    Just another idea since I am binding to a winforms combobox:

    public enum DateRange {
        [Display(Name = "None")]
        None = 0,
        [Display(Name = "Today")]
        Today = 1,
        [Display(Name = "Tomorrow")]
        Tomorrow = 2,
        [Display(Name = "Yesterday")]
        Yesterday = 3,
        [Display(Name = "Last 7 Days")]
        LastSeven = 4,
        [Display(Name = "Custom")]
        Custom = 99
        };
    
    int something = (int)DateRange.None;
    

    To get the int value from the display name from:

    public static class EnumHelper<T>
    {
        public static T GetValueFromName(string name)
        {
            var type = typeof(T);
            if (!type.IsEnum) throw new InvalidOperationException();
    
            foreach (var field in type.GetFields())
            {
                var attribute = Attribute.GetCustomAttribute(field,
                    typeof(DisplayAttribute)) as DisplayAttribute;
                if (attribute != null)
                {
                    if (attribute.Name == name)
                    {
                        return (T)field.GetValue(null);
                    }
                }
                else
                {
                    if (field.Name == name)
                        return (T)field.GetValue(null);
                }
            }
    
            throw new ArgumentOutOfRangeException("name");
        }
    }
    

    usage:

    var z = (int)EnumHelper<DateRange>.GetValueFromName("Last 7 Days");
    
    0 讨论(0)
  • 2020-12-04 09:02
    enum Constants
    {
        Abc = 1,
        Def = 2,
        Ghi = 3
    }
    
    ...
    
    int i = (int)Enum.Parse(typeof(Constants), "Def");
    
    0 讨论(0)
  • 2020-12-04 09:08

    If using 4.5+ Framework I would use ReadOnlyDictionary (also ReadOnly Collection for lists) to do readonly mappings/constants. It's implemented in the following way.

    static class SomeClass
    {
        static readonly ReadOnlyDictionary<string,int> SOME_MAPPING 
            = new ReadOnlyDictionary<string,int>(
                new Dictionary<string,int>()
                {
                    { "One", 1 },
                    { "Two", 2 }
                }
            )
    }        
    
    0 讨论(0)
提交回复
热议问题