Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码合同:如何抑制此“合同”;“需要未经证实的”;警告_C#_.net 4.0_Code Contracts - Fatal编程技术网

C# 代码合同:如何抑制此“合同”;“需要未经证实的”;警告

C# 代码合同:如何抑制此“合同”;“需要未经证实的”;警告,c#,.net-4.0,code-contracts,C#,.net 4.0,Code Contracts,我有一段代码,cccheck告诉我,Requires()未经验证,我应该添加一个!string.IsNullOrWhitespace(…)。当我调用自己在.Net 3.5时代编写的扩展方法时,已经检查了该条件: public static bool IsEmpty(this string s) { if (s == null) return true; if (s.Length == 0) return true; for (int i = 0; i < s.Len

我有一段代码,
cccheck
告诉我,
Requires()
未经验证,我应该添加一个
!string.IsNullOrWhitespace(…)
。当我调用自己在.Net 3.5时代编写的扩展方法时,已经检查了该条件:

public static bool IsEmpty(this string s)
{
    if (s == null) return true;
    if (s.Length == 0) return true;
    for (int i = 0; i < s.Length; i++)
        if (!char.IsWhitespace(s[i]))
            return false;
    return true;
}

public static bool IsNotEmpty(this string s)
{
    return !IsEmpty(s);
}
我怎样才能告诉
cccheck
(以及代码契约框架的其余部分)
IsNotEmpty()
已经检查了
!string.IsNullOrWhitespace(…)

尝试
Contract.Contract(Contract.Result()==!string.IsNullOrWhitespace))

编辑:

是的,当我发布它时,我意识到这会导致“未经证实”,我希望能找到一些时间来更彻底地回答。如果您能够忍受扔掉旧代码,那么有一种(有些琐碎的)修复方法:

public static bool IsEmpty(this string s) 
{ 
    Contract.Ensures(Contract.Result() == string.IsNullOrWhitespace(s))
    return string.IsNullOrWhitespace(s);
} 

public static bool IsNotEmpty(this string s) 
{ 
    Contract.Ensures(Contract.Result() == !string.IsNullOrWhitespace(s))
    return !string.IsNullOrWhitespace(s);
} 

忽略我在实现中看到的问题,我很好奇为什么要实现自己版本的IsNullOrWhitespace()。也许现在是重新考虑您的方法的好时机…?因为编写
value.IsEmpty()
string.IsNullOrWhitespace(value)
所需的击键次数更少。在
IsNullOrWhitespace
被添加到
string
之前,我编写了
IsEmpty()
扩展方法
IsNullOrEmpty
在早期的项目中是不够的。我将该方法作为
StringHelpers
静态类的一部分保留下来,该类中充满了扩展方法。因为我的webhost不支持.NET4,所以我仍然在较新的项目中使用该类@RickLiddle您指的是什么问题?这会导致“确保未经验证”而不是之前的“需要未经验证”。:(然而,我认为这是一条正确的道路。一个可行的解决方案!如果我将它与中给出的解决方案结合起来,如果我在为.Net 3/3.5编写的另一个项目中引用StringHelpers.csproj,我可以保留我的旧代码。
public static bool IsEmpty(this string s) 
{ 
    Contract.Ensures(Contract.Result() == string.IsNullOrWhitespace(s))
    return string.IsNullOrWhitespace(s);
} 

public static bool IsNotEmpty(this string s) 
{ 
    Contract.Ensures(Contract.Result() == !string.IsNullOrWhitespace(s))
    return !string.IsNullOrWhitespace(s);
}