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!
Alternatively referred to as a wild character or wildcard character, a wildcard is a symbol used to replace or represent one or more characters. The most common wildcards are the asterisk (*), which represents one or more characters and question mark (?) that represents a single character.
A wildcard pattern is a series of characters that are matched against incoming character strings. You can use these patterns when you define pattern matching criteria. Matching is done strictly from left to right, one character or basic wildcard pattern at a time.
A wildcard character is a type of meta character . In various games of playing cards, a wild card is a designated card in the deck of cards (for example, the two of spades) that can be used as though it were any possible card.
If you want to do wildcard expansion in such places, you need to use the wildcard function, like this: $(wildcard pattern ...) This string, used anywhere in a makefile, is replaced by a space-separated list of names of existing files that match one of the given file name patterns.
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>(); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With