Forum guidelines
AuthorQuestion
difference between string.empty and string="" c#
Posted By Ajitender Vijay on 20 Dec 2012
difference between string.empty and string="" c#

Thanks
Ajitender vijay
-----------------------------
If this post helps you, then mark as "Correct Answer" Thank You
AuthorReply
Re: difference between string.empty and string="" c#
Posted By Sivaraman Dhamodaran on 20 Dec 2012  
Both are same. Write a simple two statements yourself, one with .empty and one with = "". Then put a break point in the first statement. Call for Dis-assembly, and you will not see any difference. That's why they both are same.

You can choose whatever is more readable to you 

Laziness is nothing but taking rest before get tired Web: www.mstechArticles.com
Re: difference between string.empty and string="" c#
Posted By arjun on 20 Dec 2012  
hi, 

    string.empty not holds any thing...string="" is having one undisplayed charecter 'space' \0.
string.empty is best way...its reduce memory and garbage.


Re: difference between string.empty and string="" c#
Posted By Vulpes on 20 Dec 2012  
String.Empty is a static readonly field of the System.String class.

When the String class is first accessed, its static constructor sets its Empty field equal to the string literal "".

So, Sivaraman is correct that they both represent an empty string i.e. a string which contains no characters, not even '\0', and so has a Length of zero.

However, they are treated differently by the compiler in that "" is a constant but String.Empty is not. You can see this by trying to compile the following program:

using System;

class Test
{  
   static void  Main()
   {
     const string s1 = "";
     const string s2 = String.Empty;
   }
}

The error message is :

error CS0133: The expression being assigned to 's2' must be constant

Notice also that strings are always followed in memory by a null character ('\0') even though this null character is not part of the string itself. This enables you to iterate through the characters of a string using a pointer in 'unsafe' code, though there can be problems if the string itself contains embedded nulls.

Re: difference between string.empty and string="" c#
Posted By Sandeep Mhatre on 24 Jun 2013  

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.