473,725 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shall I now always use String.Empty instead of ""

In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Let me know your thoughts.

Nov 23 '06 #1
26 2793
"an******@hotma il.com" wrote:
In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?
If you're checking for an empty string it's better to use:
String.IsNullOr Empty(strString Name)
As of .NET 2.0
Nov 23 '06 #2
Hi,

There is very little difference between the two.

"" is easy to write, easy to read, and near impossible to misunderstand.
It will however create an object where String.Empty would not, but the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.

Neither will detect a null string so if you can have null strings you need
to either check for that as well or use String.IsNullEr Emtpy as Leon
pointed out.

if(str == null || str == "") // or
if(String.IsNul lOrEmpty(str))

If, on the other hand, you know you will never get null strings, testing
for String.Length == 0 is the fastest way.

On Thu, 23 Nov 2006 10:57:06 +0100, <an******@hotma il.comwrote:
In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Let me know your thoughts.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 23 '06 #3
<an******@hotma il.comwrote:
In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?
Which do you find easier to read? That's pretty much the only
difference of any significance, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '06 #4
Morten Wennevik wrote:
"" is easy to write, easy to read, and near impossible to misunderstand.
It will however create an object where String.Empty would not, but the ""
object is reused throughout the lifespawn of your application so the
difference can therefore be ignored.
I was surprised to read that "" will create an object where
String.Empty will not - surely String.Empty is just a static field
that points to an interned "" constant?

Sure enough, Reflector shows that String.Empty is implemented as

class string
{
static readonly string Empty = "";
}

.... yet the below code shows that while both "" and String.Empty are
interned, "" is not reference-equal to String.Empty. What gives? I
thought the intern table was maintained across assembly boundaries, so
it shouldn't matter that String.Empty comes from mscorlib.dll while ""
comes from the app assembly.

//

using System;

namespace StringEmptyTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLi ne(Object.Refer enceEquals(Stri ng.Empty, ""));
Console.WriteLi ne(String.IsInt erned("") != null);
Console.WriteLi ne(String.IsInt erned(String.Em pty) != null);
Console.ReadLin e();
}
}
}
--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Nov 23 '06 #5
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?

Gabriel

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
<an******@hotma il.comwrote:
>In the past I always used "" everywhere for empty string in my code
without a problem.

Now, do you think I should use String.Empty instead of "" (at all
times) ?

Which do you find easier to read? That's pretty much the only
difference of any significance, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 27 '06 #6
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated. I don't alter my programming style in order to suit
situations like that - it's like changing your style to suit a
situation where garbage collection doesn't occur.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #7
See:
http://msdn2.microsoft.com/en-us/lib...attribute.aspx
http://msdn2.microsoft.com/en-us/lib...laxations.aspx

Gabriel Lozano-Morán
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
And what happens if string interning is disabled? Doesn't this mean that
each time you have a "" in your code that you will have the overhead of
allocating memory on the heap and creating a new string object?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated. I don't alter my programming style in order to suit
situations like that - it's like changing your style to suit a
situation where garbage collection doesn't occur.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #8
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote
And what happens if string interning is disabled?
String interning isn't something you can enable or disable, as far as
I'm aware - and at that point, the C# language specification is being
violated.
You can't disable it. At least not that I was able to find, and I looked for
quite a while.

I wrote up a blog entry on my findings with string interning and when /
where it can save memory (at least in the case i'm worried about):
http://www.coversant.com/dotnetnuke/...=88&EntryID=24

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
Nov 28 '06 #9
Gabriel Lozano-Morán <ab***@frontbri dge.comwrote:
See:
http://msdn2.microsoft.com/en-us/lib....compilerservi
ces.compilation relaxationsattr ibute.aspx
How bizarre. Using that would certainly mean you were no longer using a
language which conformed to the C# language spec. However, you would
have to *explicitly* use that attribute, so again I can't see that it's
actually a consideration to think about in normal coding.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #10

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

Similar topics

8
2144
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
5818
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
4
2682
by: Marcin Dobrucki | last post by:
I've been having some problems with a parse error that I can't figure out (PHP 4.3.11 on Solaris9). Sample code: <?php // getting strange parse errors on this class A { var $value; function A() { $this->value = 1; }
8
4858
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,
9
3781
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in code. for example, this is bad practice: header.h #include <string> using std::string;
10
24180
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
10
13640
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
4593
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?
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
21
2971
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
0
8752
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
9401
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
9257
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
9111
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
8096
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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
4517
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...
1
3221
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
3
2157
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.