c# - 我应该使用 Nullables 还是结构的唯一值?
问题描述
我正在重构我的程序,从类切换到结构,以便从中获得更多性能。我需要一个值或方法来替换我的代码中的“null”。C# 提供了Nullable
类型(即StructName?
),但它看起来更像是一个用于包装其他类型的特殊类,而不是我可以信任以保持性能的某种特殊空值。
Node? temp = _closedList[_closedList.IndexOf(current)];
if (temp == null as Node) return null;
do {
_path.Push(temp.Value);
RecordState();
temp = getNode(temp.Value.ParentPosition);
} while (temp.HasValue && temp.Value.Position != startNode.Position);
我的另一个可能更好的主意是创建一个表示“null”的特殊值,Int32.MaxValue
对所有相关字段使用类似的东西,然后使用一个检查这些特殊值的 getter。据我所知,这听起来会保留性能,但看起来不会超级好。
Node temp = _closedList[_closedList.IndexOf(current)];
if (temp.IsNull) return null;
do {
_path.Push(temp);
RecordState();
temp = getNode(temp.ParentPosition);
} while (!temp.IsNull && temp.Position != startNode.Position);
我已经查看了很多不同的消息来源,一个说 Nullables 很糟糕,另一个说它们很好,还有人建议使用独特的值作为替代。我应该怎么办?
注意:上面的代码没有经过测试,我只是编辑了我认为 Nullable/Null Value 实现可能看起来的样子。
解决方案
推荐阅读
- python-3.x - 我可以使用 pybind 加载外部 c++ dll 并调用函数吗?
- docker - Docker Swarm 托管节点上的 Docker Engine 版本升级,无需停机
- javascript - 使用 rhino 在 java 中检索 javascript 对象
- python - 是否有与 contr.sum 的 R 平均表等效的 Python 函数
- reactjs - 反应变化变量 onChange 方法
- javascript - Microsoft office.js Excel 加载项 - 使用 javascript/react 检索工作表/工作簿唯一 ID
- arrays - 在 Julia 中初始化一个数组
- java - 创建一个具有可变数量的泛型类型的类
- docker - SF4 + docker:Proxymanager 错误:语法错误,意外的“?”,期望函数 (T_FUNCTION) 或 const (T_CONST)
- javascript - setState 对象数组而不更改对象顺序