Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a wildcard in C#

Tags:

c#

generics

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!

like image 636
ElfsЯUs Avatar asked Sep 29 '12 02:09

ElfsЯUs


People also ask

What is a wildcard in C?

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.

What is a wildcard pattern?

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.

What is wildcard give example?

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.

How do you use a wildcard in makefile?

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.


1 Answers

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>(); }
}
like image 80
Alexei Levenkov Avatar answered Sep 24 '22 00:09

Alexei Levenkov