编辑列表<元组>项目</tuple>

时间:2014-10-04 02:24:03

标签: c# list tuples

使用List<String>,您可以使用以下内容编辑项目:

var index = List.FindIndex(s => s.Number == box.Text);
List[index] = new String;

但是,如何在List<Tuple<string, string>>上应用它?

2 个答案:

答案 0 :(得分:12)

您可以通过类似的方式找到索引 - 通过将条件应用于列表中的每个元组:

var index = listOfTuples.FindIndex(t => t.Item1 == box1.Text || t.Item2 == box2.Text);

您可以致电Tuple.Create

来替换项目
listOfTuples[index] = Tuple.Create(box3.Text, box4.Text);

答案 1 :(得分:6)

var tuple = List.Find(s => s.Item1 == box.Text); 
//assuming you're searching for the first string, but you can change the predicate anyway. 
tuple = new Tuple<string, string>(new String, tuple.Item2);

正如在另一个答案中所提到的,你当然可以使用索引,但你也可以找到对象并更新它,这也应该有效。