Monday, 12 January 2009

C# overloading index operator

class MyClass
{
  private readonly Dictionary<string,object> data = new Dictionary<string,object>();
  public object this [string index]
  {
    set{ data[index] = value; }
    get{ return data[index]; }
  }
  
  public static void Main()
  {
    MyClass me = new MyClass();
    me["test"] = "sample value";
    Console.WriteLine(me["test"]);
  }
}

No comments:

Post a Comment