设置私人词典

3

我有一个关于 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

需求似乎非常技术性。如果您当前的解决方案有效,为什么不使用它呢?如果它无效,那么具体是哪些方面出了问题,您期望得到什么样的结果呢? - nvoigt
1
如果你现在的解决方案有效,为什么不使用它呢?这是一个令人担忧的声明。 - Jonesopolis
选择 Dictionary<string, int> docTypeValue = new Dictionary<string, int>();,然后使用 ctrl+x 剪切并粘贴到方法上方 :) - Sriram Sakthivel
@Sriram 很简单!除了在 click 方法中构建 ".Add" 部分,还有更好的构建位置吗? - Jon Harding
@JonHarding 当然,等一下。 - Sriram Sakthivel
5个回答

7
您需要的是像这样的东西。使用集合初始化器功能:Collection Initializer
private Dictionary<string, int> docTypeValue = new Dictionary<string, int> 
{ 
    { "OSD", 1 },
    {"POD", 2},
    {"REC", 3},
    {"CLAINF", 4},
    //...
};

2
如果你不想让非成员修改字典的内容,但又想让它可用,可以像这样操作:
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"]);
}

我很抱歉如果这是一个简单的答案,但是最好的地方添加键/值是哪里? - Jon Harding
1
在类的任何其他地方,类成员仍然可以访问dictionary,但是外部调用者无法访问。我会更新我的答案... - sircodesalot

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
}

或者是这些的组合。

在这两种情况下,您的字典将只初始化一次,与您当前的方法相比。


0
如果它是一个私有成员,你不需要使用属性,只需使用“-”。
private Dictionary<string, int> _docTypeValue;

0
根据我对你的需求“我需要将字典设置为私有字典并检查值。”的理解,你需要在类级别上拥有此字典,你不需要为该字典创建属性,只需将其创建为私有字典即可。
与上面的答案类似。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接