- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读过 When should I use a struct instead of a class?其中引用了 MSDN 的指南,其中说:
Do not define a structure unless the type has all of the following characteristics:
- It logically represents a single value, similar to primitive types (integer, double, and so on).
- It has an instance size smaller than 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.
和Why are mutable structs “evil”?有答案:
Structs are value types which means they are copied when they are passed around.
我很难理解为什么我不应该只意识到结构需要通过引用传递,否则对通过函数传递的版本所做的更改不会影响原始版本。是的,过去我遇到过很多这样的错误,但现在我有足够的经验,所以我会小心翼翼地避免它发生。
我专门用 C# 制作游戏,游戏中的变量需要进行大量更改。
假设我有一个玩家角色,在我的游戏中用类“Player”表示。如果 Player 有变量 X 和 Y 来说明它在哪里,这些就会发生很大变化。我不想每次移动都创建一个新的 Player,只是为了保持不变性?所以我的类(class)应该是可变的。好的。
但是如果我想存储关于我的实体的数据怎么办。如果我将它在屏幕上的位置存储在一个 Rectangle (x,y,w,h) 中,我是否应该将 Rectangle 设为 CLASS 因为它是可变的?它只是 4 个变量的逻辑集合,因此结构体作为容器对我来说似乎是明智的。
我也将“Colour”作为结构 (r,g,b,a),但我将其设置为可变的,因为我可能想改变游戏中事物的 alpha 以淡入/淡出,或颜色当它受到伤害时会出现一秒钟的红色,而无需在每次渲染调用时调用“新颜色”的开销。
我看到另一篇文章说如果满足这些条件就应该使用结构:
- Is the main responsibility of the type data storage?
- Is its public interface defined entirely by properties that access or modify its data members?
- Are you sure your type will never have subclasses?
- Are you sure your type will never be treated polymorphically?
对于我使用的结构,答案是"is",但其中大部分都是可变的。
我对这里所有相互矛盾的建议感到困惑。可变结构是否适合某些类型的使用,还是我设计的代码完全错误?
最佳答案
这不仅仅是意外丢失信息的机会:
list.ForEach(item => item.X = 10);
// this code does nothing useful if item is a mutable struct
也是the weird interaction between mutating methods and readonly
:
readonly MutableStruct m_field;
...
m_field.MutatingMethod(); // mutates a temporary copy rather than the field
但是如果您通过分析确定:
那么可变结构可能就是您所需要的。这就是他们毕竟在语言中的原因。
说到游戏代码,SharpDX 充满了可变结构 ( example ) 和通过引用传递的方法。
关于c# - 另一个 "Why shouldn' t 结构是可变的?”C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810404/
为什么禁用类型像 type t = A of int | B of string * mutable int 虽然允许此类类型: type t = A of int | B of string * i
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
我需要转到引用的结构: class SearchKnot { var isWord : Bool = false var text : String = "" var to
如sec 10.4.3中所述 当控制进入执行时,执行以下步骤 功能对象F(调用者)中包含的功能代码的上下文 提供thisArg,而调用方提供argumentsList: 如
i make a game that start display Activity indicator And activity indicator bottom display UiLable wi
编辑:我在这里不断获得支持。只是为了记录,我认为这不再重要。自从我发布它以来我就不再需要它了。 我想在 Scala 中执行以下操作... def save(srcPath: String, destP
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
如果您在Kotlin中访问List类型的Java值,则将获得(Mutable)List!类型。 例如。: Java代码: public class Example { public stati
我编写了 str 类(内置)的以下扩展,以便执行以下操作:假设我有字符串 "Ciao" ,通过做"Ciao" - "a"我想要的结果是字符串 "Cio" 。这是执行此操作的代码,并且运行良好: cla
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
我正在为我的公司设计一个数据库来管理商业贷款。每笔贷款都可以有担保人,可以是个人或公司,在借款业务失败时作为财务支持。 我有 3 个表:Loan、Person 和 Company,它们存储明显的信息。
我使用二进制序列化从 C# 类中保存 F# 记录。一切正常: F#: type GameState = { LevelStatus : LevelStatus
import javax.swing.JOptionPane; public class HW { public static void main(String[] args) { Strin
使用 flatbuffer mutable 有多少性能损失? 是否“正确”使用 FlatBuffers 来拥有一个应该可编辑的对象/结构(即游戏状态) 在我的示例中,我现在有以下类: class Ga
std::function create_function (args...) { int x = initial_value (args...); return [x] () mut
我需要在 for 循环中找到用户输入的字符。我通常会这样做 如果(句子[i] == 'e') 但是因为在这里,'e' 将是一个单字母字符变量,我不知道如何获取要比较的值。我不能只输入 if (sent
我有一个这样的算法: let seed: Foo = ... let mut stack: Vec = Vec::new(); stack.push(&seed); while let Some(ne
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
如果我有以下结构: struct MyStruct { tuple: (i32, i32) }; 以及以下函数: // This will not compile fn function(&mut s
我希望在每个 session 的基础上指定列的默认值。下面的脚本不起作用,但描述了我想如何使用它。我目前使用的是 MySQL 5.5.28,但如果需要可以升级。 CREATE TABLE my_tbl
我是一名优秀的程序员,十分优秀!