Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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# 缺少IsNullOrEmptyOrWhiteSpace方法_C#_.net - Fatal编程技术网

C# 缺少IsNullOrEmptyOrWhiteSpace方法

C# 缺少IsNullOrEmptyOrWhiteSpace方法,c#,.net,C#,.net,我定义一个字符串并通过string.IsNullOrEmptyOrWhiteSpace()检查它 但我有一个错误: “string”不包含“IsNullOrEmptyOrWhiteSpace”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“IsNullOrEmptyOrWhiteSpace”(是否缺少using指令或程序集引用?)D:\project\project\Controllers\aController.cs 23 24 project 原因是什么?已在.NET 4中

我定义一个字符串并通过
string.IsNullOrEmptyOrWhiteSpace()
检查它

但我有一个错误:

“string”不包含“IsNullOrEmptyOrWhiteSpace”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“IsNullOrEmptyOrWhiteSpace”(是否缺少using指令或程序集引用?)D:\project\project\Controllers\aController.cs 23 24 project

原因是什么?

已在.NET 4中引入。如果您不是针对.NET 4,您可以轻松编写自己的:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(string value)
    {
        if (value != null)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
        }
        return true;
    }
}
或者,如果您愿意:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        if (value != null)
        {
            for (int i = 0; i < value.Length; i++)
            {
                if (!char.IsWhiteSpace(value[i]))
                {
                    return false;
                }
            }
        }
        return true;
    }
}

要使扩展方法工作,请确保定义了
StringExtensions
静态类的命名空间在范围内。

可能
IsNullOrWhiteSpace
是您正在搜索的方法吗

这里是另一个替代实现,只是为了好玩。它的性能可能不如Darin的,但它是LINQ的一个很好的例子:

public static class StringExtensions
{
    public static bool IsNullOrWhiteSpace(this string value)
    {
        return value == null || value.All(char.IsWhiteSpace);
    }
}

有趣的是,这里没有人使用修剪功能:

public static class StringExtensions
{
    public static bool IsNullOrEmptyOrWhiteSpace(this string value)
    {
        return string.IsNullOrEmpty(value) ||
               ReferenceEquals(value, null) ||
               string.IsNullOrEmpty(value.Trim(' '));
    }
}
更新:我从现在的评论中看到,它被提出并因各种原因被拒绝,但如果人们更喜欢简洁而不是效率…

我使用过(在.NET v2.0中):

Trim()
方法将删除所有前导或尾随空格,因此,如果字符串完全是空格,则它将减少为空字符串


我不能说性能是一个问题。

完全复制了Microsoft的.NET 4 Framework源代码,…\RefSrc\source.NET\4.0\DEVDIV\u TFS\Dev10\Releases\RTMRel\ndp\clr\src\BCL\System\String.cs\1305376\String.cs

    public static bool IsNullOrEmpty(String value) {
        return (value == null || value.Length == 0); 
    }

    public static bool IsNullOrWhiteSpace(String value) {
        if (value == null) return true; 

        for(int i = 0; i < value.Length; i++) { 
            if(!Char.IsWhiteSpace(value[i])) return false; 
        }

        return true;
    }

空白字符由Unicode标准定义。
IsNullOrWhiteSpace
方法将传递给
Char.IsWhiteSpace
方法时返回true值的任何字符解释为空白字符。

Pre.NET 4.0,最短的:

public static bool IsNullOrWhiteSpace(this string value)
{
    return value == null || value.Trim() == "";
}

效率不高;Jon的方法考虑可读性和性能更好。

@Darin,你完全正确,该方法仅在.NET 4.0和.NET Client Profile 4.0中启用。请回答一个问题,为什么使用内部for循环而不是value.trim()=string.empty?@Jon,这样您就不必创建新字符串,只要找到第一个非空白字符,就可以返回false(Trim将查看字符的开头和结尾,直到找到非空白字符)
TrimStart
没有那么糟糕,但是您仍然在不必要地创建一个新的(可能是长的)字符串。@KMan,如果扩展的
参数为空,则允许扩展方法正常运行被认为是不好的做法。在这种情况下,在扩展方法中不抛出ArgumentNullException与其他对象引用的行为不一致。这是根据微软的建议来做的。@uosɐ我想这是一个不应该抛出异常的例外情况。)@4thpage:没有“csharp”这样的东西。如果您使用VS2010,它很容易将目标框架切换到4.0,并且像魔术一样,错误将会出现disappear@John桑德斯:如果它看起来像csharp,闻起来像csharp,拼写也像csharp,那么它一定是csharp。@AMi没有csharp、c-sharp、c##或cshizzarp标记。有一个c#标记,你可以用它来标记一个c#问题。你正在寻找一个未定义的函数。string(System.string)仅包括IsNullOrEmpty或IsNullOrWhitespace的定义,而不包括isNullOrEmptyWhiteSpace的定义。如果字符串为空,则string.IsNullOrWhitespace将返回true。另外,这是一个关于.NET框架的问题,并不是c#特有的。它如何比我的版本更简洁?我还建议在这里使用| |比使用条件运算符更具可读性。@Jon,不是这样。我会克制自己不要在星期天这样发帖:)写一些简单的逻辑是多么复杂的方式啊!我很想给它-1,但它不是“错的”,只是不容易阅读。“if”语句呢?既然
.All()
在值为空时也会返回true,这可能正是OP想要的。我使用过(在.NET v2.0中):自从NET3.5中引入扩展方法以来,如何在NET2.0中使用它?最初,在VS2005中,这是一个实用方法(从第一个参数中删除“this”)。后来我们转到VS2008,但仍然以.net2.0为目标,并使用以下方法避免使用扩展方法(这是编译器功能,而不是.NET Framework功能):。现在我们在.NET3.5上,所以我们丢失了软糖。这不产生垃圾和分配吗?Trim()将生成一个新字符串,因为字符串是不可变的。为这样一个简单的操作触发分配是一种浪费。尽可能避免字符串操作。请注意,.Trim()会创建一个必须进行垃圾收集的额外字符串,而for/Char.IsWhiteSpace解决方案不会这样做。@HansKesting是的,而且Trim()需要比/All.Char.IsWhiteSpace更多的字符串遍历,因为在后者中,在第一个非空白字符之后停止执行。正如我在回答中所说的,这不是有效的,但对于90%的情况来说,这并不重要。他给出了我能想到的最简洁的回答。乔恩的回答确实是我在这里找到的最好的。
public static class StringExtensions
{
    public static bool IsNullOrEmptyOrWhitespace(this string value)
    {
        return string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim());
    }
}
    public static bool IsNullOrEmpty(String value) {
        return (value == null || value.Length == 0); 
    }

    public static bool IsNullOrWhiteSpace(String value) {
        if (value == null) return true; 

        for(int i = 0; i < value.Length; i++) { 
            if(!Char.IsWhiteSpace(value[i])) return false; 
        }

        return true;
    }
return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
public static bool IsNullOrWhiteSpace(this string value)
{
    return value == null || value.Trim() == "";
}