Generic method with type constraints or base class parameter

前端 未结 5 1138
清歌不尽
清歌不尽 2021-02-05 04:07

If I write a method accepting a parameter which derives from a BaseClass (or an interface), as far as I know there are two ways to achieve that:

voi         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 04:34

    In the examples included in your question, there isn't much difference between the generic and the non-generic version. But here are some other examples of method signatures that can't be expressed without generics:

    T MyMethod(T obj) where T : BaseClass { ... } 
    void MyMethod(T obj1, T obj2) where T : BaseClass { ... } 
    void MyMethod(T obj, List list) where T : BaseClass { ... }
    

提交回复
热议问题