473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string.empty vs. ""

Hi

I am wondering if there is any programmatic difference between the
String.Empty and the "" for declaring an empty string?

Furthermore, is there any performance penalty by doing the one over the
other?
/Ole
Nov 16 '05 #1
6 5463
Looks like there is no difference as it is just a field that holds an empty
string. There probably is no noticable different in performance since
String.Empty is simply a field.

static String()
{
string.Empty = "";
...}

"Ole Hanson" <ol********@ole.ole> wrote in message
news:eo*************@tk2msftngp13.phx.gbl...
Hi

I am wondering if there is any programmatic difference between the
String.Empty and the "" for declaring an empty string?

Furthermore, is there any performance penalty by doing the one over the
other?
/Ole

Nov 16 '05 #2
Ole Hanson wrote:
Hi

I am wondering if there is any programmatic difference between the
String.Empty and the "" for declaring an empty string?
No.
Furthermore, is there any performance penalty by doing the one over
the other?


I'd be surprised if that was the case. I assume String.Empty exists
primarily to enhance readability -- but certainly not everybody likes it (I
do, though).

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #3
String.Empty is just a "language-independent way of expressing an empty
string".
"Ole Hanson" <ol********@ole.ole> wrote in message
news:eo*************@tk2msftngp13.phx.gbl...
Hi

I am wondering if there is any programmatic difference between the
String.Empty and the "" for declaring an empty string?

Furthermore, is there any performance penalty by doing the one over the
other?
/Ole

Nov 16 '05 #4
Correct me if I'm wrong

But, I thought there was a difference.

S1 = "";
S2 = "";

Doesn't this create 2 "different" string instances.

S1 = String.Empty;
S2 = String.Empty;

Doesn't this refer twice to same static instance?

This is a very important, especially when using a lot of strings. Perhaps
the compiler solves this.

Kind regards

Alexander

"Ole Hanson" <ol********@ole.ole> wrote in message
news:eo*************@tk2msftngp13.phx.gbl...
Hi

I am wondering if there is any programmatic difference between the
String.Empty and the "" for declaring an empty string?

Furthermore, is there any performance penalty by doing the one over the
other?
/Ole

Nov 16 '05 #5
Alexander Muylaert wrote:
Correct me if I'm wrong

But, I thought there was a difference.

S1 = "";
S2 = "";

Doesn't this create 2 "different" string instances.
No, not like this.
S1 = String.Empty;
S2 = String.Empty;

Doesn't this refer twice to same static instance?
Yes.
This is a very important, especially when using a lot of strings.
Perhaps the compiler solves this.


See section 2.4.4.5 of the C# language spec:

Each string literal does not necessarily result in a new string instance.
When two or more string literals that are equivalent according to the string
equality operator [...] appear in the same assembly, these string literals
refer to the same string instance.

Cheers,

--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #6
Kewl

Misunderstanding on my behalve.

Thanks a lot

Alexander
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jaime Wyant | last post by:
Will someone explain this to me? >>> "test".find("") 0 Why is the empty string found at position 0? Thanks! jw
4
by: Klaus Petersen | last post by:
Hi ng. I have a varchar field in my table, called Name. I wanna do a selection, which is ordered by whether this field is empty or not. E.g. something like: SELECT
0
by: Dana | last post by:
I am using the XMLTextWriter to build an XML string and pass it to the XMLDocument. When I get the data from SQL Server, some of the values passed to the XML are NULL in the database. When I try...
8
by: Peter Merwood | last post by:
I'm using some sample code from MSDN. The code includes the following statement: Dim content as = .Empty I'm not familiar with the use of the square brackets. Can someone please explain to...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
6
by: =?Utf-8?B?SmVmZg==?= | last post by:
I thought this would already be covered here, but my search turned up nothing. In VS2005, if I use "String" to define a new variable/class, it colors it in the Aqua color as it does other...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.