Is there a version of the class Tuple whose Items properties are not readonly and can be set? [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-10 00:35:22

问题


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)


回答1:


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.

namespace StackOverflow.Helpers
{
    public class Pair<T1, T2>
    {
        public T1 First { get; set; }
        public T2 Second { get; set; }
    }
} 



回答2:


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.



来源:https://stackoverflow.com/questions/7787994/is-there-a-version-of-the-class-tuple-whose-items-properties-are-not-readonly-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!