String.Empty Field
Represents the empty string. This field is read-only.
Assembly: mscorlib (in mscorlib.dll)
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.
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.
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.
- 4/26/2012
- NojiRatz
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
- 11/15/2010
- Mark Phillips
- 11/11/2011
- R Petrusha - MSFT
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
- 11/9/2011
- Sib2
- 11/11/2011
- R Petrusha - MSFT