简体   繁体   English

.NET string.IsNullOrWhiteSpace实现

[英].NET string.IsNullOrWhiteSpace implementation

Folks, I was looking at the implementation of string.IsNullOrWhiteSpace in: 伙计们,我正在查看string.IsNullOrWhiteSpace的实现:

http://typedescriptor.net/browse/types/9331-System.String http://typedescriptor.net/browse/types/9331-System.String

Here is the implementation: 这是实施:

public static bool IsNullOrWhiteSpace(string value)
{
    if (value == null)
    {
        return true;
    }
    for (int i = 0; i < value.Length; i++)
    {
        if (char.IsWhiteSpace(value[i]))
        {
        }
        else
        {
            goto Block_2;
        }
    }
    goto Block_3;
    Block_2:
    return false;
    Block_3:
    return true;
}

Question: Isn't this over complicated? 问题:这不是很复杂吗? Can't the following implementation do the same job and be easier on the eye: 以下实现无法完成相同的工作并且更容易实现:

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;
}

Is this implementation incorrect? 这个实现不正确吗? Does it have a performance penalty? 它是否有性能损失?

The original code (from the reference source) is 原始代码(来自参考源)是

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;
}

You're seeing the output of a poor decompiler. 你看到一个糟糕的反编译器的输出。

You are looking at C# that was recreated from the disassembled IL. 您正在查看从反汇编的IL重新创建的C#。 I am sure that the actual implementation is closer to your example and does not use labels. 我确信实际的实现更接近您的示例,并且不使用标签。

It must be typedescriptor's disassembler doing that. 它必须是typedescriptor的反汇编程序。

When I look at the same function with JetBrain's dotPeek it looks like this: 当我使用JetBrain的dotPeek查看相同的功能时,它看起来像这样:

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

Shown below is an extension method I needed for older versions. 下面显示的是旧版本所需的扩展方法。 I am not sure where I obtained the code from: 我不确定从哪里获得代码:

public static class StringExtensions
    {
        // This is only need for versions before 4.0
        public static bool IsNullOrWhiteSpace(this string value)
        {
            if (value == null) return true;
            return string.IsNullOrEmpty(value.Trim());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net 3.5使用代码协定实现String.IsNullOrWhitespace - .Net 3.5 Implementation of String.IsNullOrWhitespace with Code Contracts LINQ 表达式中的 String.IsNullOrWhiteSpace - String.IsNullOrWhiteSpace in LINQ Expression string.IsNullOrWhiteSpace()和string.IsNullOrEmpty()中的NullReferenceException - NullReferenceException in string.IsNullOrWhiteSpace() and string.IsNullOrEmpty() C#string.IsNullOrWhiteSpace(“\\ t”)== true - C# string.IsNullOrWhiteSpace(“\t”) == true 为什么LINQ无法转换string.IsNullOrWhiteSpace()? - Why can LINQ not translate string.IsNullOrWhiteSpace()? string.IsNullOrEmpty(string)与string.IsNullOrWhiteSpace(string) - string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string) string.IsNullOrEmpty &amp; string.IsNullOrWhiteSpace 为空字符串返回 false - string.IsNullOrEmpty & string.IsNullOrWhiteSpace return false for empty string Java等价于c#String.IsNullOrEmpty()和String.IsNullOrWhiteSpace() - Java equivalent of c# String.IsNullOrEmpty() and String.IsNullOrWhiteSpace() string.IsNullOrEmpty(myString)或string.IsNullOrWhiteSpace(myString)是否违反了SRP规则? - string.IsNullOrEmpty(myString) or string.IsNullOrWhiteSpace(myString) is not violating SRP Rule? string.IsNullOrEmpty(myString.Trim()) 与 string.IsNullOrWhiteSpace(myString) - string.IsNullOrEmpty(myString.Trim()) vs string.IsNullOrWhiteSpace(myString)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM