设置私人字典

时间:2013-10-08 15:19:53

标签: c# dictionary

我有一个Dictionary<string, int>的工作示例。我需要将Dictionary设置为private Dictionary并检查值。

目前我有:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Dictionary<string, int> docTypeValue = new Dictionary<string, int>();
    docTypeValue.Add("OSD", 1);
    docTypeValue.Add("POD", 2);
    docTypeValue.Add("REC", 3);
    docTypeValue.Add("CLAINF", 4);
    docTypeValue.Add("RATE", 5);
    docTypeValue.Add("OTHER", 6);
    docTypeValue.Add("CARINV", 7);
    docTypeValue.Add("PODDET", 8);
    docTypeValue.Add("BOLPO", 9);

    litText.Text = docTypeValue[txtDocType.Text].ToString();
}

这可以按预期工作。我需要使用房产吗?即在

之下
private Dictionary<string, int> DocTypeValue
{
    get;
    set; 
}

如何重构上面的内容以创建建议的private Dictionary

5 个答案:

答案 0 :(得分:5)

你正在寻找这样的东西。使用Collection Initializer功能。

private Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    //...
};

答案 1 :(得分:1)

如果您不希望非成员能够修改字典的内容,但希望使其可用,则可以执行以下操作:

private Dictionary<String, Int32> dictionary = ...
public IEnumerable<Int32> Dictionary { get{ return dictionary.Values; } }

// Other methods in the class can still access the 'dictionary' (lowercase).
// But external users can only see 'Dictionary' (uppercase).
void AddItemToDictoinary(String key, Int32 value) {
    dictionary.Add(key, value);  // dictionary is accessible within the class.
}

或使用这样的索引器:

private Dictionary<String, Int32> dictionary = ...
public Int32 this[String key] { get { return dictionary[key]; } }

// Same as above - within the class you can still add items to the dictionary.
void AddItemToDictoinary(String key, Int32 value) {
    dictionary.Add(key, value);
}

使用索引器利用Dictionary<T, U>后面的BST(而不是使用顺序搜索)。所以如果你的字典定义如下:

class SneakyDictionary { 
    private Dictionary<String, Int32> dictionary = ...
    public Int32 this[String key] { get { return dictionary[key]; } }

    // Same as above - within the class you can still add items to the dictionary.
    void AddItemToDictoinary(String key, Int32 value) {
        dictionary.Add(key, value);
    }
}

您可以这样使用它:

public static void Main() {
    SneakyDictionary dictionary = ...
    dictionary.AddItemToDictionary("one", 1);
    dictionary.AddItemToDictionary("two", 2);
    dictionary.AddItemToDictionary("three", 3);

    // Access items in dictionary using indexer:
    Console.WriteLine(dictionary["one"]);
}

答案 2 :(得分:1)

这是一个范围问题。如果你的字典对整个类有用,你可以将它作为私有静态(或不是)只读字段用初始化器实例化:

private static readonly Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    // and so on
};

但是你也可以依赖一个名为静态构造函数的.Net特性:

private static Dictionary<string, int> docTypeValue;

static YOURCLASSNAME()
{
    docTypeValue = new Dictionary<string, int>();
    docTypeValue.Add("OSD", 1);
   // and so on
}

或者这些的组合。

在这两种情况下,您的字典将根据您当前的方法初始化一次。

答案 3 :(得分:0)

如果是私人会员,您不需要房产,只需使用 -

即可
private Dictionary<string, int> _docTypeValue;

答案 4 :(得分:0)

根据我对你的要求的理解“我需要将Dictionary设置为私有字典并检查值。”你需要在类级别拥有这个字典,你不需要为字典创建属性,只需将其创建为私有字典。

与上述答案类似。