473,765 Members | 2,061 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
21 2979
>
I personally like to use "", because sometimes you can mistake
string.Empty for being null.
It's essentially the same thing, although there might be some
difference under the hood.
But I believe that string.Empty just does = "", so it's just one more
level of redirection (although I have to check up on the value of
string.Empty)
If you have a doubt about what something does in the .NET Framework,
just google for DotNetReflector and download it. It's a great program
that uses reflection to disassemble the IL code of any .NET assembly
(including mscorlib and the like) and then convert it to C# or some
other languages (although some code is obfuscated).
Empty is a readonly member variable of the String class which is set in
String's default constructor as an empty string literal (IE: ""). But, just
because it is a member variable does NOT mean it isn't treated differently
by the compiler and compiled into more efficient code depending on how it's
called.

HTH,
Mythran
Aug 25 '08 #21
On Mon, 25 Aug 2008 09:46:14 -0700, Mythran <My*****@commun ity.nospam>
wrote:
Empty is a readonly member variable of the String class which is set in
String's default constructor as an empty string literal (IE: ""). But,
just because it is a member variable does NOT mean it isn't treated
differently by the compiler and compiled into more efficient code
depending on how it's called.
In particular, because it's an interned string in a shared DLL (that is, a
DLL that can be shared across multiple processes), there is a potential
tiny savings if one uses _only_ String.Empty, in that the process won't
have to allocate that one extra emtpy string.

If you've got even one "" in your code somewhere though, that would negate
any benefit. And in any case, it's certainly not worth worrying about.

Pete
Aug 25 '08 #22

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

Similar topics

0
2269
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
75218
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
26909
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
6941
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
9568
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
10163
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
10007
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...
1
9957
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
9835
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
7379
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
6649
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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

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.