Properly exposing a List?

前端 未结 8 1476
谎友^
谎友^ 2021-02-02 01:20

I know I shouldn\'t be exposing a List in a property, but I wonder what the proper way to do it is? For example, doing this:

public static          


        
8条回答
  •  面向向阳花
    2021-02-02 01:51

    You don't need to worry about the overhead of cloning: wrapping a collection with a ReadOnlyCollection does not clone it. It just creates a wrapper; if the underlying collection changes, the readonly version changes also.

    If you worry about creating fresh wrappers over and over again, you can cache it in a separate instance variable.

提交回复
热议问题