string.Empty与"",null的区别,用哪个更好呢?

null是C#关键字,是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,如果 int i=null,的话,是不可以的,因为Int是值类型的。

string.Empty就相当于"",但他俩和NULL是两码事。

据说是:string.Empty不分配存储空间
  ""分配一个长度为空的存储空间
所以string.Empty 比""效率要高点。(效率高是对的,但是到底是否分配存储空间,还不明确,待核实)

一个外国人做了一个测试,测试的对象有5种,分别是:

  • s == “”
  • s == string.Empty
  • s.Equals(”")
  • s.Equals(string.Empty)
  • s.Length == 0  

    通过下列的测试代码(如要看请点击下面的加号),判断谁的效率更高~

     

    #region 测速代码
    const int cnTries = 10;
    const int cnIterations = 1000000000;
    DateTime dtStart, dtEnd;
    TimeSpan ts;
    double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
    double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
    double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
    double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
    double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
    int i, j;
    string sEmpty = string.Empty;
    string sShort = “This is a short string to test empty string comparison”;
    string sLong = “This is a long string to test the efficiency of comparing with empty strings, which means it has to be like, really long. And I’m starting to run out of useless things to say…”;

    for (j = 0; j < cnTries; ++j)
    {
        
    //double fEmptyQuotes = 0, fShortQuotes = 0, fLongQuotes = 0;
        dtStart = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sEmpty == “”) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fEmptyQuotes 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sShort == “”) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fShortQuotes 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sLong == “”) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fLongQuotes 
    += ts.TotalMilliseconds;

        
    //double fEmptyEmpty = 0, fShortEmpty = 0, fLongEmpty = 0;
        dtStart = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sEmpty == string.Empty) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fEmptyEmpty 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sShort == string.Empty) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fShortEmpty 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sLong == string.Empty) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fLongEmpty 
    += ts.TotalMilliseconds;

        
    //double fEmptyDotQuotes = 0, fShortDotQuotes = 0, fLongDotQuotes = 0;
        dtStart = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sEmpty.Equals(“”)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fEmptyDotQuotes 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sShort.Equals(“”)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fShortDotQuotes 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sLong.Equals(“”)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fLongDotQuotes 
    += ts.TotalMilliseconds;

        
    //double fEmptyDotEmpty = 0, fShortDotEmpty = 0, fLongDotEmpty = 0;
        dtStart = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sEmpty.Equals(string.Empty)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fEmptyDotEmpty 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sShort.Equals(string.Empty)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fShortDotEmpty 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sLong.Equals(string.Empty)) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fLongDotEmpty 
    += ts.TotalMilliseconds;

        
    //double fEmptyDotLength = 0, fShortDotLength = 0, fLongDotLength = 0;
        dtStart = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sEmpty.Length == 0) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fEmptyDotLength 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sShort.Length == 0) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fShortDotLength 
    += ts.TotalMilliseconds;

        dtStart 
    = DateTime.Now;
        
    for (i = 0; i < cnIterations; ++i)
        
    {
            
    if (sLong.Length == 0) ;
        }

        dtEnd 
    = DateTime.Now;
        ts 
    = dtEnd - dtStart;
        fLongDotLength 
    += ts.TotalMilliseconds;
    }


    Console.WriteLine(“empty: 
    {0}”, fEmptyQuotes / (double)cnTries);
    Console.WriteLine(“
    short{0}”, fShortQuotes / (double)cnTries);
    Console.WriteLine(“
    long : {0}”, fLongQuotes / (double)cnTries);
    Console.WriteLine(“empty: 
    {0}”, fEmptyEmpty / (double)cnTries);
    Console.WriteLine(“
    short{0}”, fShortEmpty / (double)cnTries);
    Console.WriteLine(“
    long : {0}”, fLongEmpty / (double)cnTries);
    Console.WriteLine(“empty: 
    {0}”, fEmptyDotQuotes / (double)cnTries);
    Console.WriteLine(“
    short{0}”, fShortDotQuotes / (double)cnTries);
    Console.WriteLine(“
    long : {0}”, fLongDotQuotes / (double)cnTries);
    Console.WriteLine(“empty: 
    {0}”, fEmptyDotEmpty / (double)cnTries);
    Console.WriteLine(“
    short{0}”, fShortDotEmpty / (double)cnTries);
    Console.WriteLine(“
    long : {0}”, fLongDotEmpty / (double)cnTries);
    Console.WriteLine(“empty: 
    {0}”, fEmptyDotLength / (double)cnTries);
    Console.WriteLine(“
    short{0}”, fShortDotLength / (double)cnTries);
    Console.WriteLine(“
    long : {0}”, fLongDotLength / (double)cnTries);
    #endRegion

     

    最后得到测试的结果如下:

    [s == ""]

    • 空字符串, 10315.6250 毫秒
    • 短字符串, 8307.8125  毫秒
    • 长字符串, 8564.0625  毫秒

     [s == string.Empty]

    • 空字符串, 3573.4375  毫秒
    • 短字符串, 8307.8125  毫秒
    • 长字符串, 8603.1250  毫秒

    [s.Equals("")]

    • 空字符串, 9517.1875  毫秒
    • 短字符串, 7537.5000  毫秒
    • 长字符串, 7576.5625  毫秒

     [s.Equals(string.Empty)]

    • 空字符串, 9540.6250  毫秒
    • 短字符串, 7515.6250  毫秒
    • 长字符串, 7607.8125  毫秒

     [s.Length == 0]

    • 空字符串, 443.7500  毫秒
    • 短字符串, 443.7500  毫秒
    • 长字符串, 445.3125  毫秒

         很明显用字符串的length属性是最快的。

         得到以下结论:

          s.Equals("stringtocompare") 来判断非空字符串是否相等,用 s.Length == 0 来判断是否是空字符串(注意这里不能用这个来判断字符串为NULL的情况,否则会出现“未将对象引用设置到对象的实例”的错误)。

           在2.0中判断字符串是否为空(包含NULL的情况)用String.IsNullOrEmpty(str) ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值