以访问密钥为成员的C#集合

时间:2014-08-12 10:27:07

标签: c# collections enums

我想要一个C#字符串常量的集合,它必须具有:

1.Keys(定义到期编译时间),我可以访问:

Console.WriteLine(obj.Key1); // string output: Value1 
Console.WriteLine(obj.Key2); // string output: Value2

目的是让Intellisens支持(obj。\ ctrl + space \ - 键列表出现)

2.Enumeration:

foreach(var key in obj)
{
  Console.WriteLine(key);
  // output:
  //   Value1
  //   Value2
}

3.Inheritance

class A {
    obj.A = "Value1";
    obj.B = "Value2";

    public void IterateOverMyObj()
    {
        foreach (var obj in MyObj)
        {
            Console.WriteLine(obj);
            // Output: 
            //   Value1
            //   Value2
        }
    }

class B : A {
    obj.A = "Value1New";
    obj.C = "Value3NewKey";
}

void Main()
{
    B b = new B();
    b.IterateOverMyObj();
    // output:
    //   Value1New
    //   Value2
    //   Value3NewKey
}

作为价值观,我只需要字符串。这一切都可能吗?

1 个答案:

答案 0 :(得分:2)

Dictionary<K,V>HashTable怎么办?

HashTable

Dictionary

对于Keys,您可以定义常量或类似的东西

相关问题