Java Map equivalent in C#

前端 未结 3 1521
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 12:29

I\'m trying to hold a list of items in a collection with a key of my choice. In Java, I would simply use Map as follows:

class Test {
  Map         


        
3条回答
  •  终归单人心
    2021-01-30 12:54

    class Test
    {
        Dictionary entities;
    
        public string GetEntity(int code)
        {
            // java's get method returns null when the key has no mapping
            // so we'll do the same
    
            string val;
            if (entities.TryGetValue(code, out val))
                return val;
            else
                return null;
        }
    }
    

提交回复
热议问题