FOR EXPERTS: Template Class: Type Constraints
-
Hey everybody... I got a question for advanced C# programmers. I've been playing around with type constraints for template classes, and I got stuck. I used [ and ] instead of < and > for template classes because I wanted to have HTML code for you to see better what code I got. In the C# 3.0 specification (and I will quote directly from Microsoft's document), it's said that this will work:
class B[T] where T: IEnumerable {...}
class D[T]: B[T] where T: IEnumerable {...}
class E[T]: B[List[T]] {...}Because (so they say): "Since type parameters are not inherited, constraints are never inherited either. In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B[T]. In contrast, class E need not specify a constraint, because List[T] implements IEnumerable for any T." Now... I put this pre into VS 2008 and the compiler won't work. My pre:
class B[T] where T : IEnumerable[T] { }
class D[T] : B[T] where T : IEnumerable[T] { }
class E[T] : B[List[T]] { }Error: "Error 1 The type 'System.Collections.Generic.List[T]' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'System.Collections.Generic.List[T]' to 'System.Collections.Generic.IEnumerable[System.Collections.Generic.List[T]]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 46 10 FunFollAdmin" Even more......... not even this works:
B[test] test = new B[test]();
Error: "Error 1 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 9 FunFollAdmin Error 2 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 28 FunFollAdmin" Even though I have implemented the class test correctly:
class test : IEnumerable\[int\] { public test () { } #region IEnumerable\[int\] Memb
-
Hey everybody... I got a question for advanced C# programmers. I've been playing around with type constraints for template classes, and I got stuck. I used [ and ] instead of < and > for template classes because I wanted to have HTML code for you to see better what code I got. In the C# 3.0 specification (and I will quote directly from Microsoft's document), it's said that this will work:
class B[T] where T: IEnumerable {...}
class D[T]: B[T] where T: IEnumerable {...}
class E[T]: B[List[T]] {...}Because (so they say): "Since type parameters are not inherited, constraints are never inherited either. In the example below, D needs to specify the constraint on its type parameter T so that T satisfies the constraint imposed by the base class B[T]. In contrast, class E need not specify a constraint, because List[T] implements IEnumerable for any T." Now... I put this pre into VS 2008 and the compiler won't work. My pre:
class B[T] where T : IEnumerable[T] { }
class D[T] : B[T] where T : IEnumerable[T] { }
class E[T] : B[List[T]] { }Error: "Error 1 The type 'System.Collections.Generic.List[T]' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'System.Collections.Generic.List[T]' to 'System.Collections.Generic.IEnumerable[System.Collections.Generic.List[T]]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 46 10 FunFollAdmin" Even more......... not even this works:
B[test] test = new B[test]();
Error: "Error 1 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 9 FunFollAdmin Error 2 The type 'FunFollAdmin.frmMain.test' cannot be used as type parameter 'T' in the generic type or method 'FunFollAdmin.frmMain.B[T]'. There is no implicit reference conversion from 'FunFollAdmin.frmMain.test' to 'System.Collections.Generic.IEnumerable[FunFollAdmin.frmMain.test]'. D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 216 28 FunFollAdmin" Even though I have implemented the class test correctly:
class test : IEnumerable\[int\] { public test () { } #region IEnumerable\[int\] Memb
The compiler is right. You wrote
where T : IEnumerable[T]
: this means that T must be an enumeration of itself! Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable
), meaning that T can be any enumeration.modified on Friday, February 6, 2009 10:10 AM
-
The compiler is right. You wrote
where T : IEnumerable[T]
: this means that T must be an enumeration of itself! Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable
), meaning that T can be any enumeration.modified on Friday, February 6, 2009 10:10 AM
Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable[T]), meaning that T can be any enumeration.
You mean
where T : IEnumerable
Ok but how do you declare it so? Because if I declare it so, it saysError 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin
I used the code directly from MSDNinterface IMyInterface { } class Dictionary[TKey, TVal] where TKey : IComparable, IEnumerable where TVal : IMyInterface { public void Add (TKey key, TVal val) { } }
Same error as above!!! I don't get it! Oh boy......... ::- (Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
-
Note that the MSDN sample uses the non-generic version of IEnumerable (where T : IEnumerable[T]), meaning that T can be any enumeration.
You mean
where T : IEnumerable
Ok but how do you declare it so? Because if I declare it so, it saysError 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin
I used the code directly from MSDNinterface IMyInterface { } class Dictionary[TKey, TVal] where TKey : IComparable, IEnumerable where TVal : IMyInterface { public void Add (TKey key, TVal val) { } }
Same error as above!!! I don't get it! Oh boy......... ::- (Error 1 Using the generic type 'System.Collections.Generic.IEnumerable' requires '1' type arguments D:\Order\A1.FunFoll Administrator [FFA]\2009-01-25\FunFollAdmin\frmMain.cs 48 33 FunFollAdmin
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
-
You mean
where T : IEnumerable
Yes, I mistyped. Corrected now. You must add
using System.Collections
in order to use the non-generic version of IEnumerable (or you use its full name:where T : System.Collections.IEnumerable
).Ah, damn. I had using System.Collections.Generic; but not using System.Collections; Thank you Mirko! ::- ) That fixed it.
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
-
Ah, damn. I had using System.Collections.Generic; but not using System.Collections; Thank you Mirko! ::- ) That fixed it.
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?