-1

I am new to C#, How to use below generic for C#, i want the same as below Java statement in C#,

List<? extends MySuperClass> list= new ArrayList<MySubClass>();

Java allows above, can we achieve same in C#?

Anchit Pancholi
  • 1,174
  • 4
  • 14
  • 40

1 Answers1

4

C# does not have wildcards. So the simple answer is no, there is no direct equivalent.

However, I don't think there is much point in writing that line of code in Java, because you are throwing away type information. You may as well write

List<MySubClass> list = new ArrayList<MySubClass>();

The main reason for using wildcards in Java is to allow method arguments to be as general as possible, like this:

static void foo(List<? extends MySuperClass> list)

You can do that with C# like this:

static void foo<T>(IList<T> list) where T : MySuperClass 
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • how can i use this with variable like below `public AbstractIBaseBo extends AbstractDomain> abstractIBaseBo =null;` – Anchit Pancholi Oct 06 '15 at 19:17
  • @AnchitPancholi I find that design really odd. You should avoid declaring fields like that. – Luiggi Mendoza Oct 06 '15 at 19:20
  • @AnchitPancholi I don't think you can. I could be wrong as I'm not as familiar with C# as I am with Java. I think you can have an `AbstractIBaseBo`. I also think you can have a field of type `AbstractIBaseBo` in a generic class with type parameter `T` where `T : AbstractDomain`. – Paul Boddington Oct 06 '15 at 19:21
  • i will try to find out other possible solution, else i need to change my design, most possibly . – Anchit Pancholi Oct 06 '15 at 19:28