Solved

What is the difference between null & String.Empty

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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