.NET字典作为属性

时间:2009-06-11 13:15:05

标签: c# .net dictionary properties

有人可以指出我的一些C#代码示例或提供一些代码,其中Dictionary已被用作Class的属性。

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

8 个答案:

答案 0 :(得分:37)

这是一个简单的例子

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";

答案 1 :(得分:16)

示例代码:

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]);

答案 2 :(得分:11)

您还可以查看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不是字典,除非为包装类实现它们,否则不能在其上使用任何字典方法。但在某些情况下,索引器是一个很好的工具。

答案 3 :(得分:4)

为了确保封装正确并且无法使用Add或表单ExampleDictionary [1] =&#34; test&#34;在类外部更新字典,请使用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;

答案 4 :(得分:2)

将字典用作仅具有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}
    }          

此结构可用于替换值。

答案 5 :(得分:1)

一个例子......

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);
    }
}

答案 6 :(得分:1)

.net 4.6 起,您还可以定义以下字典:

private Dictionary<string,int> Values => new Dictionary<string, int>()
{
    { "Value_1", 1},
    { "Value_2", 2},
    { "Value_3", 3},
};

它叫Expression-bodied members

答案 7 :(得分:0)