str=="" str.Length==0 str==String.Empty三种方法判断字符串为空,哪一种更快?

本文通过测试str=="", str.Length==0, str==String.Empty三种方式判断字符串为空,结果显示str.Length==0最快,其次是str==String.Empty,最慢的是str==""。" 97135505,6009560,Java并发:AtomicInteger实现线程安全计数,"['Java并发', '线程安全', 'AtomicInteger']
摘要由CSDN通过智能技术生成

str==""

str.Length==0

str==String.Empty 

这是三种用来判断字符串是否为空的方法,那么这三种方法哪一种执行起来更快呢?

为了得出结果,我在vs.net 2005中写了下面这一小段程序来进行判断:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Test
    {
        public static void Main()
        {
            string str = "ljdfskldfsklj";
            //System.Diagnostics.Stopwatch提供了一组方法和属性,可以准确地测量运行时间
            Stopwatch sw;
            sw=Stopwatch.StartNew();
            if (str == "") ;
            Console.WriteLine("str==/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值