Labels

Friday, May 23, 2008

String.Empty vs String. Length: And why String.IsNullOrEmpty

String.Empty vs String. Length: And why String.IsNullOrEmpty

Comparing against String.Length == 0 is faster.

Reason:

· Length is stored and need not to be calculated.

· Numeric operations are in itself faster than String

Though it is faster, it will throw an exception in case the string is null.

Input

String.Length Results

Null

Format Exception

Empty string (“”)

0

So overall better is String.IsNullOrEmpty(someString). It safely works as below.

· On Null strings as no exception is thrown

· On Empty strings as no object object instance is created. See Post.

Reference: http://www.velocityreviews.com/forums/t101572-stringlenght-vs-stringempty.html

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment