Solved

What is the difference between null & String.Empty

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access-Control-Allow-Origin 4 46
How does this modal gets closed? 6 42
Easiest intro into .NET CORE? 2 34
How do you think this website does searches? 5 36
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 …
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.
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 implement Singleton Design Pattern in Java.

710 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