[英]Get set on a property for a dictionary?
简单地设置属性代码:
private int time;
public int Time
{
get
{
return time;
}
set
{
time= value;
}
}
我怎样才能为字典做呢? 还是不可能?
private Dictionary<string, float> times = new Dictionary<string, float>();
一个常见的约定是使用私有字段:
private Dictionary<string, float> _times;
public Dictionary<string, float> times
{
get
{
if (_times == null)
{
// Initilize the dictionary
_times = new Dictionary<string, float>();
_times.Add("bla", 45F);
//... it will happen one time because _times will be null only one time
// to make the code more orginized you can call some method tha will initilize the dictionary
// for example InitTimes();
}
return _times;
}
set
{
// The private _times field will be set and when you call times the get method will return _times
_times = value;
}
}
首先创建字典,就像我用值创建的一样,你可以创建空白作为-
私有 static Dictionary<string, bool> PressedShortKey = new Dictionary<string, bool> { { "C", false }, { "V", false }, {"X",false }, { "S",false},
{ "P",false}, { "Z",false}, { "Y",false}, { "B",false}
};
并且可以获取或设置 Dictionary 的值,例如 - PressedShortKey["C"] = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.