473,549 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string = "" or = string.Empty?

string = "" or string = string.Empty?
should is the proper way?

Sami
Aug 22 '08 #1
21 2929
On Aug 22, 2:36*pm, "Sami" <sam...@ymail.c omwrote:
string = "" or string = string.Empty?
should is the proper way?

Sami
string str = string.Empty is the proper and more efficient way.

Thanks,
Aug 22 '08 #2
as*****@woh.rr. com wrote:
On Aug 22, 2:36 pm, "Sami" <sam...@ymail.c omwrote:
>string = "" or string = string.Empty?
should is the proper way?

Sami

string str = string.Empty is the proper and more efficient way.

Thanks,
Well, it's not more efficient as they are both string literals, but the
code more closely resembles the intention.

If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.

--
Göran Andersson
_____
http://www.guffa.com
Aug 22 '08 #3
On Aug 22, 3:09*pm, Göran Andersson <gu...@guffa.co mwrote:
asha...@woh.rr. com wrote:
On Aug 22, 2:36 pm, "Sami" <sam...@ymail.c omwrote:
string = "" or string = string.Empty?
should is the proper way?
Sami
string str = string.Empty is the proper and more efficient way.
Thanks,

Well, it's not more efficient as they are both string literals, but the
code more closely resembles the intention.

If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.
Aug 22 '08 #4
On Fri, 22 Aug 2008 12:46:18 -0700, <za***@construc tion-imaging.comwrot e:
>[...]
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.

I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.
You've been told wrong then. At worst, all uses of the explicit literal
"" will use the same object, due to string pooling (I think in C# they
call it interning...sam e thing).

Personally I find "" more concise, just as expressive (if not more so) as
String.Empty, and I've never in decades of programming accidently deleted
the entire contents of a string literal. So for me, "" suits my needs
just fine.

Pete
Aug 22 '08 #5
On Fri, 22 Aug 2008 13:11:51 -0700, "Peter Duniho"
<Np*********@nn owslpianmk.comw rote:
>On Fri, 22 Aug 2008 12:46:18 -0700, <za***@construc tion-imaging.comwrot e:
>>[...]
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentall y removed or something that you forgot to put there.

I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.

You've been told wrong then. At worst, all uses of the explicit literal
"" will use the same object, due to string pooling (I think in C# they
call it interning...sam e thing).
Evenif the references are in difference classes in the same project?
>
Personally I find "" more concise, just as expressive (if not more so) as
String.Empty , and I've never in decades of programming accidently deleted
the entire contents of a string literal. So for me, "" suits my needs
just fine.

Pete
Aug 22 '08 #6
Although I prefer the latter of the two, I don't think it makes much
difference.

Personally, I would use string.Length == 0.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Sami" <sa****@ymail.c omwrote in message
news:Ou******** ******@TK2MSFTN GP03.phx.gbl...
string = "" or string = string.Empty?
should is the proper way?

Sami
Aug 22 '08 #7
On Fri, 22 Aug 2008 15:04:17 -0700, Joe Cool <jo*****@home.n etwrote:
>[...] At worst, all uses of the explicit literal
"" will use the same object, due to string pooling (I think in C# they
call it interning...sam e thing).

Evenif the references are in difference classes in the same project?
Yes. The pooling isn't restricted to per-class.
Aug 22 '08 #8
Joe Cool wrote:
On Fri, 22 Aug 2008 13:11:51 -0700, "Peter Duniho"
<Np*********@nn owslpianmk.comw rote:
>On Fri, 22 Aug 2008 12:46:18 -0700, <za***@construc tion-imaging.comwrot e:
>>[...]
If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidental ly removed or something that you forgot to put there.
I've been told that string.Empty does not require any user memory, and
every time you reference "" you use a little bit more user memory to
store the literal.
You've been told wrong then. At worst, all uses of the explicit literal
"" will use the same object, due to string pooling (I think in C# they
call it interning...sam e thing).

Evenif the references are in difference classes in the same project?
Even different classes residing in different assemblies loaded in
different app domains.

Arne
Aug 23 '08 #9
Göran Andersson wrote:
as*****@woh.rr. com wrote:
>On Aug 22, 2:36 pm, "Sami" <sam...@ymail.c omwrote:
>>string = "" or string = string.Empty?
should is the proper way?

string str = string.Empty is the proper and more efficient way.

Well, it's not more efficient as they are both string literals, but the
code more closely resembles the intention.

If you always use string.Empty when you want an empty string, you know
that when you find "" in your code there is something that you
accidentally removed or something that you forgot to put there.
The classic reason for using names instead of values is
that if you want to change the value, then you only need
to change it one place.

Very good reason. But it does not apply here. You will
never want to change the value of the empty string.

And "" is actually easier to read, so I will consider that
the proper way.

You could forget to put something within the "" but you could
also forget to write something instead of String.Empty - I can
not see the difference.

Arne
Aug 23 '08 #10

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

Similar topics

0
2254
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...
2
782
by: exsuscito | last post by:
After about 30 minutes of frustration I finally discovered what was causing a general 500 internal server error and to my surprise it was something I seem to always take for granted for its simplicity. A type mismatch error trying to compare a string to a numerical value, which is something that PHP has no problem or qualms with. However,...
35
75125
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
2
2122
by: zafzal | last post by:
Hello all, I have a textbox in my asp.net (vb.net) aplicattion, I need to assign it's value to a Variable of type DATE, but it's not a required field, so sometimes I want to send a blank field. I declared a variable as a date datatype and when it´s going to receive the content of the text field, it shows the following error "Cast from
10
13617
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"
10
26889
by: Dave griffiths | last post by:
Hi all Using VB2005 on Vista with a Norwegian locale setup. The test program has 3 textboxes the sum held in txt3. Using the code below, txt2 conversion causes an error when it is left empty. The code in txt1 works OK but I am asking is there a better way of coding this Public Class Main
1
6896
by: veer | last post by:
Hi i am using the follwoing code to access the data from one mdb table into other untill the field indnt =0 ot D it works fine but when empty field ("INDNT") it show the error "Operator '=' is not defined for type 'DBNull' and string "0" Do Until CrRecset.Fields("INDNT").Value = "0" Or CrRecset.Fields("INDNT").Value = "D" Udac1 =...
0
7457
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
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. ...
0
7817
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...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
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...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.