38

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)

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
GianT971
  • 4,385
  • 7
  • 34
  • 46
  • 2
    One of the basic ideas behind a tuple is that it is immutable. If you let it be modified, it's not a tuple anymore. –  Oct 16 '11 at 23:16
  • 1
    @MauricioScheffer I don't understand why? When you have a datatale for example, you can modify the columns values, so what is bad with this? – GianT971 Oct 17 '11 at 00:34
  • @GianT971, because a tuple is supposed to be immutable by principal. You can use any generic collection class if you desire mutability. –  Oct 17 '11 at 02:48
  • I'll use the solution proposed on this post: http://stackoverflow.com/questions/7788115/class-inheritance-recreate-base-class-items-or-instance-from-a-property-of-th/7788709#7788709 – GianT971 Oct 17 '11 at 11:26
  • Used [this answer](http://stackoverflow.com/a/7788709/610380) to solve the problem – GianT971 Mar 06 '12 at 14:20

2 Answers2

72

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; }
    }
} 
Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
  • 3
    Sorry, at this point your solution is not suitable forwhat I need, because it lacks some functions Tuple implement: Equals, GetHashCode, CompareTo, etc. – GianT971 Oct 16 '11 at 23:26
  • 107
    @GianT971. I'm speechless. – Ritch Melton Oct 16 '11 at 23:34
  • 22
    @GianT971, are you completely unable to do those things yourself? *It's not that hard!* Also, your question made no mention of such things being required. –  Oct 16 '11 at 23:37
  • 1
    Indeed, I will update the post...Actually I am not used to those Interfaces: IStructuralEquatable, IStructuralComparable, especially with generic types... – GianT971 Oct 16 '11 at 23:46
  • 1
    @GianT971 - Those interfaces are used with Tuples primarily. I'd be afraid to lean on a helper type for those things and would prefer provide custom equality, and ordering comparers as dictated by my model. But, it is done by Tuple, so your mileage may vary. – Ritch Melton Oct 16 '11 at 23:48
  • 7
    @GianT971 - Implementing `GetHashCode()` for a mutable type? - Risky that. See e.g. http://stackoverflow.com/q/873654/499721 – bavaza Jan 17 '14 at 15:38
  • I agree with GianT971. The second comment with many upvotes of "I'm speechless" and the third with "It's not that hard to do it yourself!" both miss the point. Implementing Tuple would be easy, so why does the framework provide it? Oh, for the same exact reason it *should* or *might* provide a mutable version of Tuple... so you don't have to write it yourself! It's a lot of code to write versions of the class with 1 item, 2 items, 6 items, etc. The framework provides all these for Tuple. If you need a mutable version, the framework simply lacks one. – Triynko Jun 25 '18 at 15:44
  • 1
    This question is dated. They've added a mutable tuple type and quite a bit more. – Ritch Melton Jun 26 '18 at 00:06
  • @RitchMelton Which type it is ? I'm actually using a Tuple and can't modify its value. Can you tell me what's the name of the mutable tuple object they've added ? I'm using .NET Framework 4.6.1 – M. Ozn Jun 28 '18 at 08:00
  • 1
    https://blogs.msdn.microsoft.com/mazhou/2017/05/26/c-7-series-part-1-value-tuples/ – Ritch Melton Jun 30 '18 at 10:15
5

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.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • 1
    Hmm if a class has a GetHashCode method and an object that implement it changes, the value returned by GetHashCode will change too, won't it? – GianT971 Oct 17 '11 at 11:28
  • 2
    @GianT971 - Yes and that's generally considered a bad thing. http://stackoverflow.com/questions/462451/gethashcode-guidelines-in-c-sharp http://stackoverflow.com/questions/873654/overriding-gethashcode-for-mutable-objects-c – Ritch Melton Oct 17 '11 at 12:49
  • 1
    @GianT971 - http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx - As always, Eric Lippert's blog is a good read. – Ritch Melton Oct 17 '11 at 12:50