Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is the difference between null & String.Empty

Posted on 2008-10-26
5
Medium Priority
?
1,570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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 800 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

704 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