我编写代码以生成应该是图形编辑器的网格。网格值包含在字典中。这是我用于生成字典对象的方法,可让您了解正在使用的对象。
public Dictionary<Tuple<int, int>, string> GenerateTable(int _x, int _y)
{
int total = _x * _y;
var grid = new Dictionary<Tuple<int, int>, string>(); //!Might need this later!
for (int i = 1; i <= _x; i++) // outer loop is column
{
for (int ii = 1; ii <= _y; ii++) // Inner loop is row -
{
grid.Add(Tuple.Create(i, ii), "O");
}
}
return grid; // Should have same amount of elements as int total
}
我有另一种方法想要更改字典中的一个元素,因为我使用的是元组键,所以我不知道要在索引中提供什么来更改值。这是另一种方法。
public void ColorPixel(Dictionary<Tuple<int, int>, string> _table, int _x, int _y, string _c)
{
foreach(var pixel in _table
.Where(k => k.Key.Item1 == _x && k.Key.Item2 == _y))
{
}
//var tbl = _table.
// Where(t => t.Key.Item1 == _x && t.Key.Item2 == _y)
// .Select(t => t.Value == _c);
}
有谁知道我如何通过访问Tuple类型的键来更改字典中的元素?
Tuple
类型是“结构上可比较的”。这意味着要访问以“ 1”为键的字典中的值,您将创建一个新的元组实例,并以您认为合适的任何方式(索引器TryGetValue
等)访问该值。
var key = Tuple.Create(x, y);
var value = dictionary[key];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句