Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. public class FooKeyedCollection : KeyedCollection<FooType,Foo>
  2. {
  3. }
  4.  
  5. public enum FooType
  6. {
  7. FooType1,
  8. FooType2,
  9. FooType3
  10. }
  11.  
  12. public Foo
  13. {
  14. public FooType Type { get; set; }
  15. public string Value { get; set; }
  16. }
  17.  
  18. public static void main()
  19. {
  20. FooKeyedCollection fkc = new FooKeyedCollection();
  21.  
  22. Foo myFoo = new Foo();
  23. myFoo.Type = FooType.FooType3;
  24. myFoo.Value = "someValue";
  25.  
  26. foo.Add( myFoo );
  27.  
  28. Foo myFooByType = foo[FooType.FooType3];
  29. Foo myFooByIndex = foo[0]; // <-- exception thrown here
  30. }
  31.  
  32. public new Foo this[int index]
  33. {
  34. get
  35. {
  36. IList<Foo> self = this;
  37. return self[index];
  38. }
  39. set
  40. {
  41. (IList<Foo>)[index] = value;
  42. }
  43. }
  44.  
  45. int index = 0;
  46. Foo myFooByIndex = foo[index];
  47.  
  48. static class CollectionUtils
  49. {
  50. public static TValue GetByIndex<TKey,TValue>
  51. (this KeyedCollection<TKey, TValue> collection, int index)
  52. {
  53. return collection[index];
  54. }
  55. }
Add Comment
Please, Sign In to add comment