-2

I would like to have a C# collection of string constants, which must have:

1.Keys (defined due compiled time), which i could access like:

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

The purpose is to have Intellisens support (obj.\ctrl+space\ - keys list appears)

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
}

As values i need strings only. Is it all possible?

Doctor Coder
  • 1,001
  • 1
  • 10
  • 17
  • 2
    Are you trying to recreate a [Dictionary](http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx)? ([Example usage](http://www.dotnetperls.com/dictionary)) – Sayse Aug 12 '14 at 10:30
  • You're looking for something similar to `Dictionary` with `dynamic` keys (the *dot* notation). Take a look at this blog post: [dynamic dictionary](http://www.aaron-powell.com/posts/2010-06-28-dynamic-dictionaries-with-csharp-4.html) – Zbigniew Aug 12 '14 at 10:33
  • You could start with [this answer](http://stackoverflow.com/a/424414/592111) and add the functionality to list the keys off. – Jon Egerton Aug 12 '14 at 10:37
  • This is sealed class, it cannot be inheritable. – Doctor Coder Aug 13 '14 at 06:54

1 Answers1

2

what about Dictionary<K,V> or HashTable ?

HashTable

Dictionary

For the Keys you could define constants or something like that

S.L.
  • 1,056
  • 7
  • 15
  • I'd like to add keys in derived classes in one action, not two (first: add key to dictionary/hashtable, second: add corresponding constant). – Doctor Coder Aug 12 '14 at 11:50