In .NET, what is the difference between String.Empty
and \"\"
, and are they interchangable, or is there some underlying reference or Localization i
Use String.Empty
rather than ""
.
This is more for speed than memory usage but it is a useful tip. The
""
is a literal 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! I don't see any memory penalties here. 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 inString.Empty
?
Reference: String.Empty vs ""