Best practice: How to expose a read-only ICollection

前端 未结 7 1714
礼貌的吻别
礼貌的吻别 2020-12-16 02:39

I have an ICollection called foos in my class which I want to expose as read-only (see this question). I see that the interface defines a

7条回答
  •  天命终不由人
    2020-12-16 02:56

    My recommendation is to return use a ReadOnlyCollection for the scenario directly. This makes the usage explicit to the calling user.

    Normally I would suggest using the appropriate interface. But given that the .NET Framework does not currently have a suitable IReadOnlyCollection, you must go with the ReadOnlyCollection type.

    Also you must be aware when using ReadOnlyCollection, because it is not actually read-only: Immutability and ReadOnlyCollection

提交回复
热议问题