08/03/30 23:32:42
>>358
IList<>とIDictionary<>のインデクサの汎用ラッパー作ってみた
public static class Indexer {
public static DictionaryIndexer<TKey, TValue> GetIndexer<TKey, TValue>(this IDictionary<TKey, TValue> dict) { return new DictionaryIndexer<TKey, TValue>(dict); }
public static ListIndexer<T> GetIndexer<T>(this IList<T> list) { return new ListIndexer<T>(list); }
}
public sealed class DictionaryIndexer<TKey, TValue> {
private IDictionary<TKey, TValue> dict;
internal DictionaryIndexer(IDictionary<TKey, TValue> dict) { this.dict = dict; }
public TValue this[TKey key] { get { return dict[key]; } set { dict[key] = value; } }
}
public sealed class ListIndexer<T> {
private IList<T> list;
internal ListIndexer(IList<T> list) { this.list = list; }
public T this[int index] { get { return list[index]; } set { list[index] = value; } }
}