Tuesday, February 7, 2012

"" vs String.Empty

String.Empty is faster than "" because it doesn't create a new string object instance. Creating new instances brings penalties on both execution speed and memory usage.

Example:
// Slowest
str1 == "";
// Slow
str1.Equals(" ") == false
//Fast
str1 == String.Empty
// Fastest
str1.Equals(String.Empty) == false

No comments:

Post a Comment

Hash Table in C#

 The Hashtable is a non-generic collection and used to store the key/value pairs based on the hash code of the key. Key will be used to acce...