0 out of 2 rated this helpful - Rate this topic

String.Empty Field

Represents the empty string. This field is read-only.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static readonly string Empty

The value of this field is the zero-length string, "".

In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is either null or String.Empty, use the IsNullOrEmpty method.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Prevents alerts of magic numbers
In many editors (such as Notepad++, SlickEdit, or the VS IDE) using "" or even _T("") causes the editor to highlight these initializers as 'magic'.  Admittedly, some editors allow you to control this alert behavior through an Options or Preference setting.

Also, some compilers complain about initializing a String to "", and prefer L"" or even _T("").

So, initializing a String with String::Empty is both (.NET) portable and prevents the magic number alert.
Always Using String.IsNullOrEmpty To Check A String???

The recomendation to always use String.IsNullOrEmpty to check whether a String is empty seems overly rigid and at times inefficient.  For example, if I initialize a string variable to String.Empty or if I am checking the Value property of a ASP.NET HiddenControl, the string value will never be null.  So why not just check whether variable == String.Empty.  Is there some other hidden behavior going on under the covers when doing this check?

Always Use String.IsNullOrEmpty?

I think that "always" is too string a word here. Certainly there are cases particularly in which there is no need to check for a null, but there is a need to check for any empty string, or in which an empty string is a legitimate value but a null is not.

However, the recommendation exists not because String.IsNullOrEmpty offers superior performance, but because an extremely common program bug involves checking for an empty string but forgetting to check for a null reference (or vice versa). The recommendation to call IsNullOrEmpty at least makes the method call explicit. If you automatically write a call to IsNullOrEmpty in your code whenever you want to test a string, you can then decide whether it's really appropriate to call it or not in each particular situation. Somehow, code like s1 == null or s1 == "" often just doesn't seeem to have the same effect; many developers forget about empty strings when testing for nulls, or forget about nulls when testing for empty strings.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation

What's the point

What's the point of using string.Empty instead of "" which is shorter. Any compiler optimizations?

A Matter of Preference

Whether you choose to use String.Empty or "" is really a matter of personal preference and programming style. For all practical purposes, they offer identical performance.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation