473,748 Members | 2,551 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 2974
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
2268
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...
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, this continuous problem continues to haunt me year after year and has brought me to tears with...
35
75217
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
2136
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
13641
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
26908
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
6936
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 = CrRecset.Fields("UDACCD").Value Indt1 = CrRecset.Fields("INDNT").Value RecId =...
0
8832
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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...
1
6799
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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
2
2791
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.