How to make a reference type property “readonly”

后端 未结 8 684
青春惊慌失措
青春惊慌失措 2020-12-28 20:23

I have a class Bar with a private field containing the reference type Foo. I would like to expose Foo in a public property, but I do n

8条回答
  •  旧时难觅i
    2020-12-28 20:37

    If you don't want anyone to mess with your state...don't expose it! As others have said, if something needs to view your internal state, provide an immutable representation of it. Alternatively, get clients to tell you to do something (Google for "tell don't ask"), instead of doing it themselves.

提交回复
热议问题