473,651 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5496
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******** *****@tk2msftng p13.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******** *****@tk2msftng p13.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******** *****@tk2msftng p13.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

Misunderstandin g 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
2136
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
5810
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
2261
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 and run an update to database using the same XML string, (using SQL parameters to pass the selectsinglenode), the XML always shows the NULL values as an empty string "". This is then updating the database with an empty value rather than keeping...
8
4852
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 me what the difference between "Dim content as " and "Dim content as String". Thanks. Peter,
10
13626
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 like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
6
4590
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 classes. But if I use "string", it colors it in Navy blue as in C# reserved words (e.g. private, void, foreach, etc). What is the diff?
21
2955
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.