如何向 CodeContracts 证明 IEnumerable<T>.Single() 永远不会 returns null?
How to prove to CodeContracts that IEnumerable<T>.Single() never returns null?
我有以下代码片段:
public static string returnString()
{
string[] stringList = { "a" };
if (stringList.Count() != 1)
{
throw new Exception("Multiple values in list");
}
var returnValue = stringList.Single();
Contract.Assert(returnValue != null, "returnValue is null");
return returnValue;
}
CodeContract 说:
CodeContracts: assert unproven. Are you making some assumption on Single that the static checker is unaware of?
根据我的理解,Single()
永远不会 returns null - 它 returns 要么是 IEnumerable 的唯一值,要么会引发异常。我如何向代码分析器证明这一点?
In my understanding, Single()
never returns null
不正确 -
string[] a = new string[] {null};
bool check = (a.Single() == null); // true
It returns either the exactly only value of the IEnumerable
or it throws an exception.
是正确的 - 所以如果集合只包含一个空值,那么 Single
将 return 为空。
我有以下代码片段:
public static string returnString()
{
string[] stringList = { "a" };
if (stringList.Count() != 1)
{
throw new Exception("Multiple values in list");
}
var returnValue = stringList.Single();
Contract.Assert(returnValue != null, "returnValue is null");
return returnValue;
}
CodeContract 说:
CodeContracts: assert unproven. Are you making some assumption on Single that the static checker is unaware of?
根据我的理解,Single()
永远不会 returns null - 它 returns 要么是 IEnumerable 的唯一值,要么会引发异常。我如何向代码分析器证明这一点?
In my understanding,
Single()
never returns null
不正确 -
string[] a = new string[] {null};
bool check = (a.Single() == null); // true
It returns either the exactly only value of the
IEnumerable
or it throws an exception.
是正确的 - 所以如果集合只包含一个空值,那么 Single
将 return 为空。