代码之家  ›  专栏  ›  技术社区  ›  Anand Shah

.NET字典作为属性

  •  25
  • Anand Shah  · 技术社区  · 15 年前

    有人能给我指出一些C代码示例,或者提供一些代码,其中字典被用作类的属性。

    到目前为止,我看到的示例并没有涵盖所有方面,即如何将字典声明为属性、添加、删除和从字典中检索元素。

    7 回复  |  直到 11 年前
        1
  •  36
  •   ehird    12 年前

    下面是一个简单的例子

    class Example {
      private Dictionary<int,string> _map;
      public Dictionary<int,string> Map { get { return _map; } }
      public Example() { _map = new Dictionary<int,string>(); }
    }
    

    一些用例

    var e = new Example();
    e.Map[42] = "The Answer";
    
        2
  •  16
  •   Hans Kesting    14 年前

    样例代码:

    public class MyClass
    {
      public MyClass()
      {
        TheDictionary = new Dictionary<int, string>();
      }
    
      // private setter so no-one can change the dictionary itself
      // so create it in the constructor
      public IDictionary<int, string> TheDictionary { get; private set; }
    }
    

    样品使用情况:

    MyClass mc = new MyClass();
    
    mc.TheDictionary.Add(1, "one");
    mc.TheDictionary.Add(2, "two");
    mc.TheDictionary.Add(3, "three");
    
    Console.WriteLine(mc.TheDictionary[2]);
    
        3
  •  11
  •   DevinB    15 年前

    你也可以调查 indexers . (官方的msdn文档 here )

    class MyClass
    {
        private Dictionary<string, string> data = new Dictionary<string, string>();
    
        public MyClass()
        {
            data.Add("Turing, Alan", "Alan Mathison Turing, OBE, FRS (pronounced /ˈtjʊ(ə)rɪŋ/) (23 June, 1912 – 7 June, 1954) was a British mathematician, logician, cryptanalyst and computer scientist.")
            //Courtesy of [Wikipedia][3]. Used without permission
        }
    
        public string this [string index]
        {
            get
            {
                return data[index];
            }
        }
    }
    

    然后,一旦在内部填充了字典,就可以通过

    MyClass myExample = new MyClass();
    
    string turingBio = myExample["Turing, Alan"];
    

    编辑

    显然,必须小心使用,因为 MyClass 不是字典,除非为包装类实现字典方法,否则不能对其使用任何字典方法。但在某些情况下,索引器是一个很好的工具。

        4
  •  4
  •   user3202856    11 年前

    为了确保封装正确,并且不能使用add或exampleDictionary[1]=“test”形式在类外部更新字典,请使用ireadOnlyDictionary。

    public class Example
    {
        private Dictionary<int, string> exampleDictionary;
    
        public Example() 
        { 
            exampleDictionary = new Dictionary<int, string>(); 
        }
    
        public IReadOnlyDictionary<int, string> ExampleDictionary
        {
            get { return (IReadOnlyDictionary<int, string>)exampleDictionary; }
        }
    }
    

    如果使用IDictionary,则以下代码将不起作用:

    var example = new Example();
    example.ExampleDictionary[1] = test;
    
        5
  •  2
  •   Erik K.    14 年前

    另一个仅使用get访问器将字典用作静态属性的示例:

      private static Dictionary <string, string> dict = new  Dictionary   <string,string>(){            
                {"Design Matrix", "Design Case"},
                {"N/A", "Other"}
        };
    
    
        public static Dictionary <string, string> Dict
        {
            get { return dict}
        }          
    

    此结构可用于替换值。

        6
  •  1
  •   user110714    15 年前

    一个例子…

    public class Example
    {
        public Dictionary<Int32, String> DictionaryProperty
        {
            get; set;
        }
    
        public Example()
        {
            DictionaryProperty = new Dictionary<int, string>();
        }
    }
    
    public class MainForm
    {
        public MainForm()
        {
            Example e = new Example();
    
            e.DictionaryProperty.Add(1, "Hello");
            e.DictionaryProperty.Remove(1);
        }
    }
    
        7
  •  0
  •   Radu094    15 年前