Indexed Properties in C# I was challenged to implement a mechanmism to support indexed properties in a class structure. 

I wanted to avoid the need to implement an abstract property indexer class every time you want to implement a new T just because the backing store has changed. Semantically, if C# did provide indexed properties, the get/set logic would be on the class declaring the indexed property (as there would be no type).

So I implemented the following logic:

public interface IIndexedProperty&lt;TKey, TValue&gt;
{
    TValue this[TKey index] { get; set; }
}
public class IndexedProperty&lt;TKey, TValue&gt; : IIndexedProperty&lt;TKey, TValue&gt;
{
    private Func&lt;TKey, TValue&gt; Getter { get; set; }
    private Action&lt;TKey, TValue&gt; Setter { get; set; }
    public IndexedProperty(Func&lt;TKey, TValue&gt; getter, Action&lt;TKey, TValue&gt; setter)
    {
        this.Getter = getter;
        this.Setter = setter;
    }
    public TValue this[TKey index]
    {
        get
        {
            return Getter(index);
        }
        set
        {
            Setter(index, value);
        }
    }
}


An example implementation:

public class MyClass
{
    public IIndexedProperty&lt;string, string&gt; Values { get; private set; }
    private Dictionary&lt;string, string&gt; ValueDictionary { get; set; }
    public MyClass()
    {
        Values = new IndexedProperty&lt;string, string&gt;(index =&gt; ValueDictionary[index], (index, value) =&gt; ValueDictionary[index] = value);
        ValueDictionary = new Dictionary&lt;string, string&gt;();
    }
}


What he doesn't like about this is twofold:


It isn't reusable (you have to implement the logic in your defining class each time you use it)
It holds references to two delegates.


Can you see ways in which this can be improved?
This implementation allows for reusability of the indexer backing logic, as shown on MyClass two having two indexers using the same backing implementation. This pattern is much more reusable.

public abstract class PropertyIndexer&lt;TKey, TValue&gt;
{
    public abstract TValue this[TKey index] { get; set; }
}
public class MyClass
{
    private class StringDictionaryPropertyIndexer : PropertyIndexer&lt;string, string&gt;
    {
        private Dictionary&lt;string, string&gt; BackingCollection { get; set; }
        public StringDictionaryPropertyIndexer(Dictionary&lt;string, string&gt; backing)
        {
            this.BackingCollection = backing;
        }
        public override string this[string index]
        {
            get
            {
                string value;
                if (BackingCollection.TryGetValue(index, out value))
                    return value;
                return null;
            }
            set
            {
                BackingCollection[index] = value;
            }
        }
    }
    private Dictionary&lt;string, string&gt; data { get; set; }
    public PropertyIndexer&lt;string, string&gt; Data { get; private set; }
    private Dictionary&lt;string, string&gt; alternateData { get; set; }
    public PropertyIndexer&lt;string, string&gt; AlternateData { get; private set; }
    public MyClass()
    {
        data = new Dictionary&lt;string, string&gt;();
        Data = new StringDictionaryPropertyIndexer(data);
        alternateData = new Dictionary&lt;string, string&gt;();
        AlternateData = new StringDictionaryPropertyIndexer(alternateData);
    }
}