public static Point FromJson(JObject json) { if (json != null && json[] != null && json[].Type == JTokenType.Integer && json[] != null && json[].Type == JTokenType.Integer ) { ], (]); } return null; }
这个函数可以用?.运算符化简成
public static Point FromJson(JObject json) { if (json != null && json[]?.Type == JTokenType.Integer && json[]?.Type == JTokenType.Integer ) { ], (]); } return null; }
如果json["x"]为null,那么就不执行. 如果json["x"]部位null,那么就会执行.然后判断类型是否为int
所以代码可以被再次化简
public static Point FromJson(JObject json) { ]?.Type == JTokenType.Integer && json?[]?.Type == JTokenType.Integer ) { ], (]); } return null; }
?.还有一个比较大的用处在触发事件的时候
OnChanged(this, args);
为了保证安全
需要先Copy一份
var onChanged = OnChanged; if (onChanged != null) { onChanged(this, args); }
现在可以改写成这样
OnChanged?.(this, args);
好滴以上就是C#6.0的一些新特性,这些改进个人感觉都是非常使用的!C#团队辛苦咯!