Reputation: 2222
I want to do following:
public class My<T>
{
public My(G<S> g, T t)
{
// code that DOES NOT use S in any way
}
}
I'm getting 'Cannot resolve symbol S'. Is it possible to do it somehow? And if not, why? I can't change G, it's not my class.
Upvotes: 1
Views: 274
Reputation: 1668
User following implementation:
public class My<T, S>
{
public My(G<S> g, T t)
{
// Now S will be accessible.
}
}
Upvotes: 0
Reputation: 2700
Just declare S explicitly in your method :
public class My<T, S>
{
public My(G<S> g, T t)
{
// code that DOES NOT use S in any way
}
}
This will compile (except if you have some conditions on S on G class). You will have to declare what is S in any case when using the My class.
Example will G< S > = Nullable< S > :
public class My<T, S> where S : struct
{
public My(Nullable<S> g, T t)
{
// code that DOES NOT use S in any way
}
}
With this implementation, you have to define S when using the My class (in that case S is int):
var myVar = new My<string, int>(4, string.Empty);
Upvotes: 0
Reputation: 62472
Unlike member function, constructors cannot specify generic types in their declaration. To work around this you'll need to lift the generic type out of the constructor and into the class declaration:
public class My<T,S>
{
public My(G<S> g, T t)
{
// code that DOES NOT use S in any way
}
}
This may be a problem if you intend to have various constructors that take additional generic types.
Upvotes: 1
Reputation: 391346
You need to either:
<S>
or G<S>
(ie. replace it with a non-generic type, an interface, etc.)or Add S
as a generic parameter to the type:
public class My<T, S>
{
public My(G<S> g, T t)
{
// code that DOES NOT use S in any way
}
}
Upvotes: 1