Guest User

Untitled

a guest
May 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. namespace InferenceBug
  5. {
  6. public class MyDictionary: IEnumerable<int>, IEnumerable<KeyValuePair<string, int>>
  7. {
  8. public MyDictionary()
  9. {
  10. _source = new Dictionary<string, int>();
  11. }
  12.  
  13. private readonly IDictionary<string, int> _source;
  14.  
  15. IEnumerator<KeyValuePair<string, int>> IEnumerable<KeyValuePair<string, int>>.GetEnumerator()
  16. {
  17. return _source.GetEnumerator();
  18. }
  19.  
  20. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  21. {
  22. return _source.Values.GetEnumerator();
  23. }
  24.  
  25. IEnumerator IEnumerable.GetEnumerator()
  26. {
  27. return _source.GetEnumerator();
  28. }
  29. }
  30.  
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. // one implementation of IEnumerable: works fine
  36. Test(new Dictionary<string, int>
  37. {
  38. ["A"] = 1,
  39. ["B"] = 2
  40. });
  41.  
  42. // two implementations: fails
  43. Test(new MyDictionary());
  44. }
  45.  
  46. private static void Test<T>(IEnumerable<KeyValuePair<string, T>> arg)
  47. {
  48. // does nothing
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment