Solved

What is the difference between null & String.Empty

Posted on 2008-10-26
5
1,487 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

706 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

21 Experts available now in Live!

Get 1:1 Help Now