String.IsNullOrEmpty is great, right?

by Miha Markič 22. June 2006 16:13

Do you know of String.IsNullOrEmpty and do you use it? You do? Think again after reading this blog post. Ooops, it might not work and even worse, it might not work only in release (optimized) configuration. Apparently JIT optimizer is to blame. What a nightmare for a developer - the same program might not work deployed at client and worse, the bug is not easy to catch either.

But now, here comes the cherry on the cake. MS recognizes this bug and they will fixes it soon, post Orcas. Note that Orcas is targeted for late 2007 or something.

I guess I'll have to replace all my String.IsNullOrEmpty calls with text == null || text == string.Empty.

Tags:

.net

Comments (8) -

Mlad
Mlad
6/22/2006 7:39:21 PM #

everything is solved with

string s = string.Empty;
if (s.Length == 0)

always declare your string as string.empty and check for length...

Null strings should be in the code if you ask me anyways...

Reply

Miha Markic
Miha Markic
6/23/2006 12:28:26 AM #

Hi,

You did mean "Null strings should *not* be in the code if you ask me anyways..."
Why not? Null has a special meaning - it means that the string isn't set. That's why MS introduced nullable types, too.
And your solution makes potentially unnecessary assignments Smile

Reply

Hugo
Hugo
6/23/2006 10:58:14 AM #

I've tested the code. When you do something in the then-part like:
   Console.WriteLine("x is null or empty");
then everything seems to be OK.
So, it obviously has something to do with optimization.

Reply

Mladen
Mladen
6/23/2006 3:45:50 PM #

yeah i meant it "not in the code"

well i hate nulls Smile)
i think that they shouldn't exist at all Smile)
It's just more work to handle them....
but i also know that they are necesary.

I tend to always declare strings a string.Empty at the begining.
gives me less grief in future development.

Reply

Miha Markic
Miha Markic
6/23/2006 6:34:18 PM #

Mladen,

Of course, as long as you have a consistent strategy its fine. And apparently your way is good, since you won't encounter the bug in question Smile

Reply

Miha Markic
Miha Markic
6/23/2006 6:35:34 PM #

Hugo,

Yup, seems so, even MS admits it. However, they didn't say what exactly is going on and how serious it is.

Reply

Andrej Tozon
Andrej Tozon
6/26/2006 11:16:52 PM #

I like nullable strings. Well, used to like them anyway... Smile

It's always good to set the string to its initial value, because most of the time you know how you're going to build it and/or what value it is supposed to hold, when the procedure returns. However, if string is an input parameter to your proc, then you don't have much choice... I'd prefer "text == null || text.Length == 0" over "text == null || text == string.Empty" though... But I'm nitpicking now...

Reply

Miha Markic
Miha Markic
8/14/2006 1:37:40 PM #

Note: The last comment was accidently lost. Sorry.

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS