- 在UI界面我进行了分层,model,view,control
- model层加载完数据,一般以方法的形式直接将List或者Dic返回给Control
- 这样就遇到一个问题,我直接用不存在的ID去Dic里面取对象,结果没有判断,然后说给出的key不存在
- 因此我的建议是将Dic包装成一个方法,根据ID返回相应的数据,如果没有对应ID则返回默认值。就用到了TryGetValue
//代码如下
public MapItem GetData(int id)
{
MapItem temp = null; //如果不存在id,则返回默认值null
mapDic.TryGetValue( id, out temp );
return temp;
}