Thursday, 14 May 2009

C# Indexer example

public class SampleIndexer
{
private readonly Dictionary<string,Dictionary<string, string>> list = new Dictionary<string,Dictionary<string, string>>();

public SampleIndexer()
{
list.Add("One",new Dictionary<string, string>());
list.Add("Two", new Dictionary<string, string>());
}

public void Add(string name, string key, string value)
{
list[name].Add(key,value);
}

public Dictionary<string,string> this[string name]
{
get
{
return list[name];
}
}

public string this[string name, string key]
{
get
{
return list[name][key];
}
}
}

[TestFixture]
public class TestIndexer{

[Test]
public void Test()
{
SampleIndexer indexer = new SampleIndexer();
indexer.Add("One","key1","value1");
indexer.Add("Two","key2","value2");

Console.WriteLine(indexer["One"]["key1"]);
}
}

No comments:

Post a Comment