相关疑难解决方法(0)

空条件运算符评估bool不bool?正如所料

我刚刚从VS 2010升级到2015.我喜欢新的零条件运算符,也称为零传播.这样可以简化代码,例如:

string firstCustomerName = customers?[0].Name; // null if customers or the first customer is null
Run Code Online (Sandbox Code Playgroud)

另一个:

int? count = customers?[0]?.Orders?.Count();  // null if customers, the first customer, or Orders is null
Run Code Online (Sandbox Code Playgroud)

返回一个Nullable<int>偶数,Enumerable.Count返回一个int以区分有效计数和nulls之前的任何一个.这非常直观,非常有用.

但是为什么这会按预期编译和工作(它返回false):

string text = null;
bool contains = text?.IndexOf("Foo", StringComparison.CurrentCultureIgnoreCase) >= 0;
Run Code Online (Sandbox Code Playgroud)

它应该返回bool?(它没有)或不编译.

.net c# c#-6.0

23
推荐指数
1
解决办法
3633
查看次数

标签 统计

.net ×1

c# ×1

c#-6.0 ×1