Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all. i have a tuple (exam tuple<string,int> iTuple=new tuple<string,int>("A",1)).
I want to change value how iTuple.Item2=3. How change? Thanks all
Posted
Updated 5-Jan-22 7:18am

A Tuple<> is immutable. So you have to create a new one if you want to change the value:
C#
Tuple<string, int> iTuple = new Tuple<string, int>("A", 1);
iTuple = new Tuple<string, int>(iTuple.Item1, 3);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-15 13:41pm    
Sure, a 5.
—SA
Thomas Daniels 15-May-15 13:43pm    
Thank you.
Maciej Los 15-May-15 14:29pm    
5ed!
Thomas Daniels 15-May-15 14:49pm    
Thank you.
In addition to solution 1 by ProgramFox[^], i'd suggest to read this very interesting article: C# 4 - Tuples[^]. MSDN migh be helpful too: Tuple Class[^]

Note: Tuples are very useful, but you have to work on specific data type, for example custom class. You can "transform" tuple to custom data type (class) via using Linq query. See: Using your own defined type in a LINQ query expression[^]

Let's say, you have custom class as follow:
C#
public class Whatever
{
    private string sProp = string.Empty;
    private int iVal = 0;

    public Whatever()
    {
    }

    public Whatever(string _Prop, int _Val)
    {
        sProp = _Prop;
        iVal = _Val;
    }

    public string Prop
    {
        get{ return sProp; }
        set{ sProp = value; }
    }
    public int Val
    {
        get{ return iVal; }
        set{ iVal = value; }
    }
}


Now, you want to create list of your data... and change first element:
C#
//list of tuples
List<Tuple<string, int>> MyTuple = new List<Tuple<string, int>>()
        {
            new Tuple<string, int>("A", 1),
            new Tuple<string, int>("A", 10),
            new Tuple<string, int>("A", 100)
        };

//transform to custom class collection
List<Whatever> ClassCollectionFromTupple = (from mt in MyTuple
    select new Whatever
        {
            Prop = mt.Item1,
            Val = mt.Item2
        }).ToList();

//get specific element
Whatever wr = ClassCollectionFromTupple[0];
//change value
wr.Val = 3;


Try! And Good Luck!
 
Share this answer
 
Comments
Thomas Daniels 15-May-15 14:50pm    
+5!
Maciej Los 15-May-15 15:10pm    
Thank you ;)
Possibly a bit overkill for most situations, but you could consider wrapping the (tuple) items with a container type - the Tuple<> indirectly referencing each of the items (via the container type/instance), whereby you can change the container's (item) value (- just not the container itself - that is immutably referenced by the parent Tuple<> / what it directly sees as been Item1 & Item2).
 
Share this answer
 
Comments
Richard Deeming 6-Jan-22 5:11am    
If the tuple wraps the container, you'd end up having to use tuple.Item1.PropertyA, tuple.Item1.PropertyB, etc. But in that case, why bother with the tuple? Just store a list of the container type instead.

There's no way to make tuple.Item1 a reference to a property on an instance of another type. The value will always be copied when the tuple is constructed, and changes to the source property will not be reflected in the tuple. You can't use C#'s ref returns[^] with the Tuple<> classes.
Richard Deeming 6-Jan-22 5:12am    
Besides, for modern code, you should be using value tuples instead. And those are mutable, so you wouldn't need a container type at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900