Why “Properties that return arrays are prone to code inefficiencies”?

断了今生、忘了曾经 提交于 2019-12-05 02:27:10

The problem is that arrays are always mutable. That means you can't return one from a method without either:

  • Allowing the caller to mess up your internal state
  • Creating a copy first

If you use a collection, you can create a read only wrapper around the real collection instead, and return that - and that can be considerably cheaper. Alternatively, if you change it to a method that will lower the expectation that it will be very quick to call.

Of course, if you're happy with callers mutating your data, then an array will work fine...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!