【发布时间】:2021-08-02 05:06:03
【问题描述】:
在以下情况下是否可以做类似的事情:dictTupleTest[key].Item1 = toggle;?
Dictionary<int, (bool, bool)> dictTupleTest = new Dictionary<int, (bool, bool)>();
var key = 3;
var toggle = false;
dictTupleTest.Add(key, (true, false));
//This works
dictTupleTest[key] = (toggle, dictTupleTest[key].Item2);
//While this gives an error
dictTupleTest[key].Item1 = toggle;
错误:Error CS1612: Cannot modify the return value of 'Dictionary<int, (bool, bool)>.this[int]' because it is not a variable.
或者有更好的方法吗?
【问题讨论】:
-
Built-in Tuples 是不可变的。一旦创建,它们就是只读的。因此,您需要更改整个设置。
-
使用类对象代替元组并定义你的属性。
标签: c# dictionary tuples