Guest User

Untitled

a guest
Aug 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Why do generic type restrictions have to be redeclared on descendant types?
  2. interface IGenericType<T> where T : new()
  3.  
  4. class GenericTypeImplementation<U> : IGenericType<U>
  5.  
  6. class GenericTypeImplementation<U> : IGenericType<U> where U : new()
  7.  
  8. interface IGenericType<T> where T : new()
  9. interface IGenericType2<T> where T : SomeOtherType
  10. class GenericTypeImplementation<U> : IGenericType<U>, IGenericType2<U>
  11. /* Hypothesis: Compiler can't infer U must be "SomeOtherType + new()" */
  12.  
  13. Error 1 The type 'U' must have a public parameterless constructor in order to use it as parameter 'T' in the generic type or method '....IGenericType<T>'
  14. Error 2 The type 'U' must be convertible to '....SomeOtherType' in order to use it as parameter 'T' in the generic type or method '....IGenericType2<T>'
  15.  
  16. class GenericTypeImplementation<U> : IGenericType<U>, IGenericType2<U>
  17. where U : SomeOtherType, new()
  18. {...}
  19.  
  20. class GenericTypeImplementation : IGenericType<SomeType>, IGenericType2<SomeOtherType>
  21. {...}
  22.  
  23. class Implementation<T> : IGenericType<List<T>> { /* ... */ }
  24.  
  25. interface ISomething {
  26. void DoIt<T>() where T : new();
  27. }
  28.  
  29. class OneThing : ISomething {
  30. public void DoIt<T>() where T : new() { }
  31. }
  32.  
  33. class OtherThing : ISomething {
  34. void ISomething.DoIt<T>() { }
  35. }
Add Comment
Please, Sign In to add comment