三种判断字符串为空方法的优先级

我们通过一个方法判断 (1.str == ""  2.str == String.Empty  3.str.Length == 0)

static void Main()
        {
            string str = "csdn";
            System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str == "")
                { }
            }
            st.Stop();
            Console.WriteLine("str == '' : " + st.ElapsedMilliseconds);
            st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str == String.Empty)
                { }
            }
            st.Stop();
            Console.WriteLine("str == String.Empty : " + st.ElapsedMilliseconds);
            st = new System.Diagnostics.Stopwatch();
            st.Start();
            for (int i = 0; i < 100000000; i++)
            {
                if (str.Length == 0)
                { }
            }
            st.Stop();
            Console.WriteLine("str.Length == 0 : " + st.ElapsedMilliseconds);
            Console.ReadKey();
        }


F5 用行
str == '': 1951  (微妙为单位)
str =='': 1867
str =='': 522

你可能感兴趣的:(职场,休闲,三种判断字符串为空方法的优先级)