Difference between interface as type constraint and interface as parameter?

前端 未结 5 1346
暖寄归人
暖寄归人 2021-02-13 14:30

If I wanted to create a method that takes an instance of IList as a parameter (or any other interface, but let\'s use IList as an example), I could cre

5条回答
  •  逝去的感伤
    2021-02-13 15:11

    Aside from all the lower-level implications, when the list is encapsulated and there are operations to modify it, you're talking about an object and this list shouldn't be exposed (in most cases), therefore the use of generics are pointless and expose, without a valid reason, the inner workings of the class.

    If you have a data structure that needs to expose the list, then specifying it by generics tend to make the data presence easier to read (IMHO).

提交回复
热议问题