开发者

Class constraints JAVA to C# implemetation

开发者 https://www.devze.com 2023-03-24 12:08 出处:网络
Java implementation: I have this abstract class: public abstract class Base<T> {} and the derived:

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> { }
0

精彩评论

暂无评论...
验证码 换一张
取 消