Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FooKeyedCollection : KeyedCollection<FooType,Foo>
- {
- }
- public enum FooType
- {
- FooType1,
- FooType2,
- FooType3
- }
- public Foo
- {
- public FooType Type { get; set; }
- public string Value { get; set; }
- }
- public static void main()
- {
- FooKeyedCollection fkc = new FooKeyedCollection();
- Foo myFoo = new Foo();
- myFoo.Type = FooType.FooType3;
- myFoo.Value = "someValue";
- foo.Add( myFoo );
- Foo myFooByType = foo[FooType.FooType3];
- Foo myFooByIndex = foo[0]; // <-- exception thrown here
- }
- public new Foo this[int index]
- {
- get
- {
- IList<Foo> self = this;
- return self[index];
- }
- set
- {
- (IList<Foo>)[index] = value;
- }
- }
- int index = 0;
- Foo myFooByIndex = foo[index];
- static class CollectionUtils
- {
- public static TValue GetByIndex<TKey,TValue>
- (this KeyedCollection<TKey, TValue> collection, int index)
- {
- return collection[index];
- }
- }
Add Comment
Please, Sign In to add comment