Solved

What is the difference between null & String.Empty

Posted on 2008-10-26
5
1,490 Views
Last Modified: 2012-05-05
Can any one explain me what is the difference between below 2 statements.

string FirstName = null;

string FirstNmae = String.Empty;

When should I use which?
0
Comment
Question by:GouthamAnand
5 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 22810256
string.Empty provides better performance when it is used against something like

""

string FristName = ""; instead of this use string FirstName = string.Empty;

0
 
LVL 6

Assisted Solution

by:slinkygn
slinkygn earned 150 total points
ID: 22810261
string FirstName = string.Empty;

creates a string object with value "".  Length is zero, etc.

string FirstName = null;

means FirstName does not refer to an object.

If you're just going to assign a different string object to FirstName, there's no point in assigning the "" null-string object to it -- might as well use string FirstName = null.

If you want to start, for example, concatenating onto FirstName, you'll want it to be a valid string object, so you'd use string FirstName = string.Empty.
0
 
LVL 8

Accepted Solution

by:
rpkhare earned 200 total points
ID: 22810483
Read below:


======== Courtesy: MSDN =============
Null Strings and Empty Strings

An empty string is an instance of a System.String object that contains zero characters. Empty strings are used quite commonly in various programming scenarios to represent a blank text field. You can call methods on empty strings because they are valid System.String objects. Empty strings are initialized like this:


string s = "";

By contrast, a null string does not refer to an instance of a System.String object and any attempt to call a method on a null string results in a NullReferenceException. However, you can use null strings in concatenation and comparison operations with other strings. The following examples illustrate some cases in which a reference to a null string does and does not cause an exception to be thrown:


string str = "hello";
string nullStr = null;
string emptyStr = "";

string tempStr  = str + nullStr; // tempStr = "hello"
bool b = (emptyStr == nullStr);// b = false;
emptyStr + nullStr = ""; // creates a new empty string
int I  = nullStr.Length; // throws NullReferenceException
==============================================

Check this link:
http://msdn.microsoft.com/en-us/library/ms228362(VS.80).aspx
0
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 150 total points
ID: 22810491
on simple way
When a string variable is set to null, this means that it doesn't currently refer to a string instance.
An empty string is a string instance which contains no characters. In other words its Length property is 0.
0
 

Author Closing Comment

by:GouthamAnand
ID: 31510202
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now