9

I needed a structure that contains a pair of values, of which ones value would be changed. So my first thought was to use a KeyValueItem or a Tupple<,> but then I saw that they have only a getter. I can't realize why? What would you use in my case? I could create my own class, but is there any other way?

Kobe-Wan Kenobi
  • 3,694
  • 2
  • 40
  • 67
  • Dictionary?? – Ric Nov 01 '13 at 10:43
  • 1
    Regarding tuple: http://stackoverflow.com/questions/7787994/is-there-a-version-of-the-class-tuple-whose-items-properties-are-not-readonly-an – dcastro Nov 01 '13 at 10:44
  • 2
    As for `KeyValuePair`, since it's a struct, it should be immutable (read http://blogs.msdn.com/b/ericlippert/archive/2008/05/14/mutating-readonly-structs.aspx). – dcastro Nov 01 '13 at 10:46
  • 1
    As others suggested, structures are generally value times/immutable thus they are not changeable unless you copy the items into a new structure with modification and return that. Are you sure you are needing value types here instead of reference types? – Ahmed ilyas Nov 01 '13 at 10:50
  • @Ric, I cannot use dictionary for some reason... You are both right for keyvalue, I missed that it is a structure. – Kobe-Wan Kenobi Nov 01 '13 at 10:55
  • Would have been ideal to store key/value pairs that can be added/removed/changed etc! – Ric Nov 01 '13 at 10:57
  • Why can't you use a Dictionary? Can you concisely describe your specific coding problem: why you need a value pair? Why one of its values needs to change? Why can you use your own mutable key/value pair class? – David Arno Nov 01 '13 at 11:00
  • @Ric, if I, in some case use a dictionary, is there a way to access Dictionary by index? I know that OrderedDictionary provides that, but that one is not generic, and it would be a lot better to have that. – Kobe-Wan Kenobi Nov 01 '13 at 11:03
  • 1
    if the key was a char and for instance you did this: Dictionary dict = new Dictionary(); and accessed it by the following: dict['c'] would return the value at the index. Check it exists first though! – Ric Nov 01 '13 at 11:04
  • That I could do, dict['c'] is completely valid, but I'm interested in this dict[3]; – Kobe-Wan Kenobi Nov 01 '13 at 11:08
  • 1
    dict[3] is not the index it is the value in which case it is possible to do this take a look at ContainsValue() here http://msdn.microsoft.com/en-us/library/a63811ah.aspx – Ric Nov 01 '13 at 11:10
  • possible duplicate of [Why Tuple's items are ReadOnly?](http://stackoverflow.com/questions/3131400/why-tuples-items-are-readonly) – nawfal Jun 04 '14 at 13:38

4 Answers4

11

They are immutable types. The idea of immutable types is that they represent a value, and so cannot change. If you need a new value, you create a new one.

Let's say the first value of your tuple needs to change, just do this:

myValue = Tuple.Create(newValue, myValue.Item2);

To understand why immutability is important, consider a simple situation. I have a class that say contains a min and max temperatures. I could store that as two values and provide two properties to access them. Or I could store them as a tuple and provide a single property that supplies that tuple. If the tuple were mutable, other code could then change these min and max values, which would mean the min and max inside my class will have changed. By making the tuple immutable, I can safely pass out both values at once, secure in the knowledge that other code can't tamper with them.

default
  • 11,485
  • 9
  • 66
  • 102
David Arno
  • 42,717
  • 16
  • 86
  • 131
2

You can create your own implementation:

public class Pair<T, U> {
    public Pair() {
    }

    public Pair(T first, U second) {
        this.First = first;
        this.Second = second;
    }

    public T First { get; set; }
    public U Second { get; set; }
};
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
0

Tuples are read only in C#. This is explained in the answer here, mainly due to their nature from functional programming.

You should create your own MutableTuple implementation that allows modification.

Things to consider:

  • You might want to override Equals and GetHashCode
  • You might want it to be sortable on the First element of the tuple (IComparable).
Community
  • 1
  • 1
Bas
  • 26,772
  • 8
  • 53
  • 86
  • Ok, I understand the reason, but I think that they could also provide something mutable like that. It's not a problem to implement it, I just wanted to know is something already there... – Kobe-Wan Kenobi Nov 01 '13 at 10:59
0

Tuples historically come from functional programming, where everything is supposed to be immutable. You can learn more about functional programming here:

Functional Programming

What are the benefits of functional programming?

And to have benefits of the historical approach, Tuples in C# have been designed the same way. If you really want mutable tuples, you can easily implement that yourself:

public class MutableTuple<TFirst, TSecond>
{
   public TFirst { get; set; }
   public TSecond { get; set; }
}
David Oganov
  • 976
  • 1
  • 11
  • 23