見註釋代碼的問題...爲什麼這一行代碼與上面兩個不同呢?
public struct Key
{
public string Name;
public object Value;
}
public class PrimaryKey
{
Dictionary<string, Key> _keys = new Dictionary<string, Key>();
object this[string index]
{
get
{
return _keys[index].Value;
}
set
{
if (!_keys.ContainsKey(index))
_keys.Add(index, new Key() { Name = index, Value = value });
else
{
var k = _keys[index]; // This compiles
k.Value = value; // just fine.
_keys[index].Value = index; // So why wouldn't this?
}
}
}
}
我得到的錯誤:
Cannot modify the return value of
Dictionary<string,Key>.this[string]
because it is not a variable
錯誤似乎在返回_keys [index] .Value; –
閱讀此篇http://stackoverflow.com/a/6255368/1714342 – wudzik