Adding generic object to generic list in C#

前端 未结 5 1271
长发绾君心
长发绾君心 2020-12-11 02:23

I have class where the relevant part looks like

class C {
    void Method(SomeClass obj) {
        list.Add(obj);
    }
    List l         


        
5条回答
  •  一整个雨季
    2020-12-11 02:38

    Personally, I would do this where possible; move the generic parameter from the method, to the class.

    class C {
        void Method(SomeClass obj) {
            list.Add(obj);
        }
        List list = new List();
    }
    

    If your generic list is a member, it stands to reason that the class should be constructed with this in mind. It is hard for us to suggest the best pattern without more usage context for the class.

提交回复
热议问题