C#'s equivalent of Java's <? extends Base> in generics

后端 未结 4 1526
北荒
北荒 2020-12-04 18:57

In Java, I can do the following: (assume Subclass extends Base):

ArrayList aList = new ArrayList

        
4条回答
  •  醉话见心
    2020-12-04 19:33

    Actually there is an Equivalent(sort of), the where keyword. I don't know how "close" it is. I had a function I needed to do something similar for.

    I found an msdn page about it.

    I don't know if you can do this inline for a variable, but for a class you can do:
    public class MyArray where T: someBaseClass
    or for a function
    public T getArrayList(ArrayList arr) where T: someBaseClass

    I didn't see it on the page but using the where keyword it might be possible for a variable.

提交回复
热议问题