C# 判断字符串为空有哪几种方法

Length法:bool isEmpty = (str.Length == 0);
Empty法:bool isEmpty = (str == String.Empty);
General法:bool isEmpty = (str == "");

2、深入内部机制: 要深入探讨其内部机制,需要查看.Net的源代码,同样有三种方法供参考: Rotor法:一个不错的选择就是微软的Rotor,这是微软的一个源代码共享项目。 Mono法:另一个不错的选择当然就是真正的开源项目Mono啦! Reflector法:最后一个选择就是使用反编译器,不过这种重组的代码不一定就是原貌,只不过是一种“近似值”,可以考虑使用Reflector这个反编译器[1]。

本文中采用的Reflector法,先来看看一下源代码[2](片段):

public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable, IComparable<string>
...{
static String()
...{
string.Empty = "";
// Code here
}
// Code here
public static readonly string Empty;
public static bool operator ==(string a, string b)
...{
return string.Equals(a, b);
}
public static bool Equals(string a, string b)
...{
if (a == b)
...{
return true;
}
if ((a != null) && (b != null))
...{
return string.EqualsHelper(a, b);
}
return false;
}
private static unsafe bool EqualsHelper(string ao, string bo)
...{
// Code here
int num1 = ao.Length;
if (num1 != bo.Length)
...{
return false;
}
// Code here
}
private extern int InternalLength();
public int Length
...{
get
...{
return this.InternalLength();
}
}
// Code here
}

Rotor里面String类的代码与此没什么不同,只是没有EqualsHelper方法,代之以如下的声明:

public extern bool Equals(String value);

进一步分析: 首先是Empty法,由于String.Empty是一个静态只读域,只会被创建一次(在静态构造函数中)。但当我们使用Empty法进行判空时,.NET还会依次展开调用以下的方法,而后两个方法内部还会进行对象引用判等!

public static bool operator ==(string a, string b);

public static bool Equals(string a, string b);

private static unsafe bool EqualsHelper(string ao, string bo);

若使用General法判等的话,情况就“更胜一筹”了!因为.NET除了要依次展开调用上面三个方法之外,还得首先创建一个临时的空字符串实例,不适合进行大量的比较。

而对于Length法,则可以绕过上面这些繁琐的步骤,直接进行整数(字符串长度)判等。 大多数情况下,整数判等都要来得快(我实在想不出比它更快的了,在32位系统上,System.Int32运算最快了)! 另外,在EqualsHelper方法里面.NET会先使用Length法来进行判等! 可惜无法获得InternalLength方法的代码。 在Mono的源代码里面看到了更简明的实现:

class String
...{
private int length;
public int Length
...{
get
...{
return length;
}
}
// .
}

然而使用Length法进行字符串判空串时,要注意,必须先判断该字符串实例是否为空引用,否则将会抛出NullReferenceException异常! 以下是改进后的Length法:

void Foo(string bar)
...{
if ((bar != null) && (bar.Length == 0))
//
}
复制代码

3、总结: 使用Length法来进行字符串判空串是有着很大的性能优势的,尤其在进行大量字符串判空时! 当然首先得判断字符串实例是否为空引用!

好了,今天的内容就介绍到这里了,C#中判断字符串是否为空的方法,肯定不止这些了,欢迎大家多多交流,有好的办法分享一下。

原文地址:https://www.cnblogs.com/testsec/p/6102617.html