3

How do I convert this "java wildcards" to C# format?

public abstract class AAA<T extends BBB<?>> extends CCC<T>{} 

My incorrect C# code

public abstract class AAA<T> : CCC<T> where T : BBB<?>{}

How do I convert BBB<?>

Biswajit_86
  • 3,661
  • 2
  • 22
  • 36
Vlad Kisly
  • 354
  • 6
  • 16

2 Answers2

1

There is a similar question C# generic "where constraint" with "any generic type" definition? not specific to Java wildcards. The answer provided there will likely help you.

From that answer, a possible class signature would be:

public abstract class AAA<T, TOther> : CCC<T>
    where T : BBB<TOther>
{}

Generics in C# must have named type parameters even if you never plan to use the type explicitly/implicitly. And all type parameters must be named in the identifier declaration of the method/class/interface.

Note that it would be possible to constrain TOther to just object types if so desired using:

public abstract class AAA<T, TOther> : CCC<T>
    where T : BBB<TOther>
    where TOther : object
{}
Community
  • 1
  • 1
Arkaine55
  • 548
  • 6
  • 15
0

What is BBB exactly?

I bet that BBB<Object> probably would work; BBB is likely a covariant type.

ZhongYu
  • 19,446
  • 5
  • 33
  • 61
  • BBB would mean that T must be a type defined as `class Something : BBB` a closed constructed generic type. Where the the requested T definition could even allow for an open constructed generic type such as `class Something : BBB` . Keep in mind that `List list = new List();` would be an error. – Arkaine55 May 12 '15 at 02:48
  • @Arkaine55 but OP is talking about C#. I suspect that here `BBB` is a covariant type, which in C#, `BBB` *is-a* `BBB` – ZhongYu May 12 '15 at 02:53
  • @bayou-io My comment is only using C#. C# 4 the variance rules with generics require more explicit definitions. http://stackoverflow.com/questions/2033912/c-sharp-variance-problem-assigning-listderived-as-listbase So if BBB is defined as `class BBB` your answer would probably be fine. I still don't like assuming what BBB is. – Arkaine55 May 12 '15 at 03:30