How to deal with the immutability of returned structs?

前端 未结 6 1974

I\'m writing a game that has a huge 2D array of \"cells\". A cell takes only 3 bytes. I also have a class called CellMap, which contains the 2D array as a private field, and pro

6条回答
  •  眼角桃花
    2021-02-04 17:29

    Encapsulate what you want the CellMap to do, and allow access to the actual array only through appropriate methods like IncrementPopupation(int x, int y). Making an array (or any variable, for that matter) public in most cases is a serious code smell, as is returning an array in .NET.

    For performance reasons, consider using a single dimensional array; those are way faster in .NET.

提交回复
热议问题