是否存在 Tuple 类的 Items 属性不是只读且可以设置的版本?
我想知道是否存在类 Tuple
的内置版本,其 Items 属性不是 readonly
并且可以设置。
或者有人可以给我一个这样的版本吗?
我正在寻找一个实现 Tuple 类基本函数的解决方案(Equals
、GetHashCode
)
I want to know whether there is a built-in version of the class Tuple
whose Items properties are not readonly
and can be set.
Or can someone provide me such a version?
I am searching for a solution that implements the base functions of the Tuple class, (Equals
, GetHashCode
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,正如前面提到的,Tuple<> 旨在是不可变的。
如果我需要一个执行相同操作的可变类型,我会使用自定义 Pair 类,尽管本着拥抱函数概念的精神,我尽量不使用它。
No, as mentioned a
Tuple<>
is intended to be immutable.I use a custom
Pair
class if I need a mutable type that does the same thing, although in the spirit of embracing function concepts, I try not to use it.由于 GetHashCode 应该为相等的实例返回相同的哈希码,并且在构造后应该是不可变的,因此任何重写 Equals 和 GetHashCode 的通用 Tuple 实现都必须具有包装项的只读属性。
因此,您不太可能找到您想要的东西。我不清楚为什么你会想要可变性和 Equals/GetHashCode 覆盖,但如果你这样做,并且了解风险,你可能必须自己动手。
Since
GetHashCode
should return the same hash code for instances that are equal, and should be immutable after construction, any general-purpose Tuple implementation that overrides Equals and GetHashCode will necessarily have readonly properties for the wrapped items.Therefore you're unlikely to find what your looking for. It's not clear to me why you would want both mutability and the Equals/GetHashCode overrides, but if you do, and understand the risks, you'll probably have to roll your own.