【发布时间】:2013-04-11 02:21:17
【问题描述】:
为什么不编译?下面的代码有什么问题?
(_DbContext == null) ? return _DbContext = new ProductAndCategoryEntities() : return _DbContext;
如果我重述它是否编译:
if (_DbContext == null)
return _DbContext = new ProductAndCategoryEntities();
else return _DbContext;
【问题讨论】:
-
_DbContext= (_DbContext == null) ? new ProductAndCategoryEntities() : _DbContext;这有效吗? -
@legendinmaking - 你完全正确,这是一个很好的解决方案
-
顺便说一句,它是“条件”运算符;就操作数的数量而言,它恰好是 三元...
标签: c# .net logic ternary-operator