Reputation: 1218
I have the following:
public class Foo<T> : Goo
where T: SomeClass<?>, new()
I know that ? is not a real wildcard in C#, however, how would you write this correctly in C# such that SomeClass can take any class as an argument? I tried using object, but then I get an error "...there is no implicit reference conversion from..."
Thanks!
Upvotes: 5
Views: 276
Reputation: 100547
You have to specify second type argument (i.e. Y
in my sample), note that Y
could be anything as there are no restrictions, even the same as T
.
public class Foo<T, Y> : Goo
where T: SomeClass<Y>, new()
Another option is to specify only second class if you only need to use SomeClass<Y>
in your generic class, you will not need new() restriction because compiler knows in advance that SomeClass<T>
have default constructor:
public class Foo<Y> : Goo{
public SomeClass<Y> Value;
public void Setup() { Value = new SomeClass<Y>(); }
}
Upvotes: 6