Class constraints JAVA to C# implemetation
Java implementation:
I have this abstract class:
public abstract class Base<T> {}
and the derived:
public class MyClass<W extends Base> {} //Compiles and works just fine
Note : in JAVA i don't need to add the generic type to Base when declaring Base as constraint.
.Net implementation
public class MyClass<W> where W : Base //Doesn't compile
public class MyClass<W> where W : Base<T> //Doesn't compile - (what is T ?)
I need to be able to declare a generic c开发者_C百科lass as constraint without specifying the generic Type (Just like in JAVA) Can i do that in .Net ???
You need to include T
in the list of generic type parameters:
public class MyClass<W, T> where W : Base<T>
Usage:
var myObject = new MyClass<ClassExtendingBase, string>();
with
public abstract class Base<T> { }
public class ClassExtendingBase : Base<string> { }
精彩评论