By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

string.empty vs. ""

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Kewl

Misunderstanding on my behalve.

Thanks a lot

Alexander
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.