Is there a .NET collection interface that prevents adding objects?

前端 未结 7 1677
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 18:13

I have a class that maintains list of objects of another class. List of objects is a public property. I would like to prevent users from adding and removing objects directly

7条回答
  •  春和景丽
    2020-12-18 18:36

    Doesn't really answer your question, but is useful information if you decide to implement one of your two ideas: You can use ToArray to create a copy and return an array.

    Or you can do something like this: http://en.csharp-online.net/CSharp_Generics_Recipes%E2%80%94Making_Read-Only_Collections_the_Generic_Way

提交回复
热议问题