Mr. T.
Mr. T.

Reputation: 1345

Compilation error when using Generics C# 3.5

The following code fails to compile:

class MyClass<T> :  where T : MyClass <T>{}

Is there any way to solve this? I have used the following workaround but I was wondering if there is a better way

class MyClass <T> : IMyClass where T : IMyClass {}
interface IMyClass {}

Upvotes: 1

Views: 173

Answers (1)

dtb
dtb

Reputation: 217233

You need to put a colon after the class name only if you want to derive the class from a base class or implement an interface:

class MyClass<T> where T : MyClass<T>
//              ↑
//              no ':' here

Upvotes: 5

Related Questions