Thursday, May 7, 2015

String.Empty Vs ""

String.Empty vs "" : An Interview Question


Use String.Empty rather than "". This is more for speed than memory usage but it is a useful tip. The "" is a literal (A literal is "any notation for representing a value within source code) so will act as a literal: on the first use it is created and for the following uses its reference is returned. Only one instance of "" will be stored in memory no matter how many times we use it!

There is no such memory penalties with the use of “”. The problem is that each time the "" is used, a comparing loop is executed to check if the "" is already in the intern pool.

On the other side, String.Empty is a reference to a "" stored in the .NET Framework memory zone. String.Empty is pointing to same memory address for VB.NET and C# applications. So why search for a reference each time you need "" when you have that reference in String.Empty?

Note: Best Coding practice is to use string.Empty.


These may be helpful down the line .Enjoy Coding and Smile Smile

MVC Articles & WCF and WebAPI

Enjoy coding and reading.


Post a Comment