如何判断字符串是否为空串?[C#]

本文围绕C#中字符串判空展开,介绍了Length法、Empty法、General法三种常用判空方法。通过查看.NET源代码深入分析其内部机制,指出Length法绕过繁琐步骤,直接进行整数判等,性能优势明显,尤其适用于大量字符串判空,但需先判断字符串实例是否为空引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何判断字符串是否为空串?[C#]

 

Written by Allen Lee

 

0. 缘起:

本文写作缘起于阮的讨论——《FxCop告诉我,检查一个字符串是否为空要用string.Length。》。其实用过FxCop的人都知道它会建议你使用String.Length属性来判断字符串是否为空串,但你又是否明白其中的缘由呢?今天有点闲,特意写下这篇文章,希望有点帮助。

1. 三种常用的字符串判空串方法:

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

2. 深入内部机制:

  • 要探讨这三种方法的内部机制,我们得首先看看.NET是怎样实现的,也就是要看看.NET的源代码!然而,我们哪里找这些源代码呢?我们同样有三种方法:
    • Rotor法:一个不错的选择就是微软的Rotor,这是微软的一个源代码共享项目。
    • Mono法:另一个不错的选择当然就是真正的开源项目Mono啦!
    • Reflector法:最后一个选择就是使用反编译器,不过这种重组的代码不一定就是原貌,只不过是一种“近似值”,你可以考虑使用Reflector这个反编译器[1]。
  • 这里我采用Reflector法,我们先来看看一下源代码[2](片段):

None.gif public   sealed   class  String : IComparable, ICloneable, IConvertible, IEnumerable, IComparable < string >
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static String()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string.Empty = "";
InBlock.gif
InBlock.gif        
// Code heredot.gif
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
// Code heredot.gif
InBlock.gif

InBlock.gif    
public static readonly string Empty;
InBlock.gif
InBlock.gif    
public static bool operator ==(string a, string b)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return string.Equals(a, b);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static bool Equals(string a, string b)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (a == b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ((a != null&& (b != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return string.EqualsHelper(a, b);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private static unsafe bool EqualsHelper(string ao, string bo)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Code heredot.gif
InBlock.gif

InBlock.gif        
int num1 = ao.Length;
InBlock.gif        
if (num1 != bo.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
// Code heredot.gif
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
private extern int InternalLength();
InBlock.gif
InBlock.gif    
public int Length
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.InternalLength();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Code heredot.gif
ExpandedBlockEnd.gif
}

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

None.gif public   extern   bool  Equals(String value);

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

None.gif public   static   bool   operator   == ( string  a,  string  b);
None.gif
None.gif
public   static   bool  Equals( string  a,  string  b);
None.gif
None.gif
private   static   unsafe   bool  EqualsHelper( string  ao,  string  bo);

    • 若使用General法判等的话,情况就“更胜一筹”了!因为.NET除了要依次展开调用上面三个方法之外,还得首先创建一个临时的空字符串实例,如果你要进行大量的比较,这恐怕是想一想就很吓人了!
    • 而对于Length法,我们就可以绕过上面这些繁琐的步骤,直接进行整数(字符串长度)判等,我们知道,大多数情况下,整数判等都要来得快(我实在想不出比它更快的了,在32位系统上,System.Int32运算最快了)!
    • 另外,我们还可以看到,在EqualsHelper方法里面.NET会先使用Length法来进行判等!可惜的是我无法获得InternalLength方法的代码。但我在Mono的源代码里面看到更简明的实现:

None.gif class  String dot.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int length;
InBlock.gif
InBlock.gif    
public int Length
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return length;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// dot.gif.
ExpandedBlockEnd.gif
}

    • 然而使用Length法进行字符串判空串时,有一点要注意的,就是你必须先判断该字符串实例是否为空引用,否则将会抛出NullReferenceException异常!于是,我们有了一个经过改进的Length法:

None.gif void  Foo( string  bar)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if ((bar != null&& (bar.Length == 0))
InBlock.gif        
// dot.gif
ExpandedBlockEnd.gif
}

3. 最后总结:

从上面的分析我们可以看到,使用Length法来进行字符串判空串是有着很大的性能优势的,尤其在进行大量字符串判空时!当然首先得判断字符串实例是否为空引用!


  1. 有关Reflector的介绍可以参见《Reflector: Get the secret inside .NET assemblies.》一文。
  2. 本代码反编译自版本号为2.0.3600.0的.NET Framework。

 

转载于:https://www.cnblogs.com/allenlooplee/archive/2004/11/11/62805.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值