问题
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