Reputation: 608
I am trying to translate this piece of code from Java to C# and I am having trouble finding the correct synthax
Java:
public class MyClass<T extends IMyInterface<T>>
My attempt at translating into C#:
public class MyClass<T, U> where T: IMyInterface<U>
If I do public class MyClass<T> where T: IMyInterface<T>
, there is no compile error at the class declaration, but I cannot figure out how to use the class.
More specifically, I have an interface IPoint<T>
, which is implemented by class EuclideanPoint:IPoint<EuclideanPoint>
. Also, I have a templated class Clusterer<U>
, which should not care about the template parameter of the interface, it should only make sure that U is of type IPoint.
Thank you.
Upvotes: 1
Views: 1503
Reputation: 1500185
No, the C# equivalent of the Java would just be:
public class MyClass<T> where T : IMyInterface<T>
One type parameter in the Java, one in the C#.
How you use the class will depend on what IMyInterface<T>
is and what implements it. For example, if it were IEquatable<T>
instead, you could create a MyClass<int>
because int
implements IEquatable<int>
.
Upvotes: 3
Reputation: 108947
class MyClass<T> where T: IMyInterface<T>
looks good.
If you have
class MySecondClass : IMyInterface<MySecondClass>
{
}
then you can use
MyClass<MySecondClass> obj = new MyClass<MySecondClass>();
Upvotes: 2