String.Empty vs “” vs String. IsNullOrEmpty
I was reading up on this recently and a couple of interesting points came to light: myString = “” creates a new instance of type String. myString = String.Empty does not. The key item is doing comparisons. Here’s some sample test code: class Program { static void Main(string[] args) { String myString = ""; long a, b, c, d, e, f; Console.WriteLine("Method 1..."); a = DateTime.Now.Ticks; for (int index = 0; index { bool isEmpty = myString == ""; } b = DateTime.Now.Ticks; Console.WriteLine("Method 2..."); c = DateTime.Now.Ticks; for (int index = 0; index { bool isEmpty = myString.Length == 0; } d = DateTime.Now.Ticks; Console.WriteLine("Method 3..."); e = DateTime.Now.Ticks; for (int index = 0; index { bool isEmpty = String.IsNullOrEmpty(myString); } f = DateTime.Now.Ticks; var Method1 = b - a; var Method2 = d - c; var Method3 = f - e; Console.WriteLine("Method 1: bool isEmpty = myString == \...