相关疑难解决方法(0)

为什么可变结构"邪恶"?

在这里讨论了SO之后,我已经多次读过可变结构是"邪恶"的评论(就像这个问题的答案一样).

C#中可变性和结构的实际问题是什么?

c# struct mutable immutability

470
推荐指数
15
解决办法
7万
查看次数

如何处理返回结构的不变性?

我正在写一个拥有巨大2D"细胞"阵列的游戏.一个单元只需3个字节.我还有一个名为CellMap的类,它包含2D数组作为私有字段,并通过公共索引器提供对它的访问.

Profiling showed that a performance problem is caused by garbage collection of too many Cell objects. So I decided to make Cell a struct (it was a class).

But now code like this doesn't work:

cellMap[x, y].Population++;
Run Code Online (Sandbox Code Playgroud)

I can think of many options, but I don't really like any of them.

  1. Make the array public, and write cellMap.Data[x, y].Population = 5;
  2. Stop using a CellMap class, and just use a 2D array directly. But CellMap is very …

.net c# language-features struct immutability

8
推荐指数
3
解决办法
472
查看次数

标签 统计

c# ×2

immutability ×2

struct ×2

.net ×1

language-features ×1

mutable ×1