Creating a constant Dictionary in C#

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

    Why not:

    public class MyClass
    {
        private Dictionary _myCollection = new Dictionary() { { "A", 1 }, { "B", 2 }, { "C", 3 } };
    
        public IEnumerable> MyCollection
        {
            get { return _myCollection.AsEnumerable>(); }
        }
    }

提交回复
热议问题