Creating a constant Dictionary in C#

前端 未结 10 2081
独厮守ぢ
独厮守ぢ 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: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 NewDictionary = new Dictionary()
            {
                { "Reference1", Array1 },
                { "Reference2", Array2 },
                { "Reference3", Array3 },
                { "Reference4", Array4 },
                { "Reference5", Array5 }
            };
    

提交回复
热议问题