473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why is an uninitialised string not empty?

Hi folks,

I came across something strange a few moments ago when testing some
strings.

In my application a particular object has a name and a title, and is
displayed on a webpage thusly:

Object name - title

But the " - title" bit is only displayed, obviously, if it is
appropriate, ie. a title has been set for that object. I attempted to
achieve that by testing with:

if (myObject.Title != string.Empty) {
...
}

Yet this didn't work as the Title property was returning 'null'. So it
got me wondering, why is 'null != string.Empty' when the 'null' value
belongs to an object of type string?

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Jun 19 '06 #1
6 3568
Dylan Parry wrote:

[...]
why is 'null != string.Empty' when the 'null' value
belongs to an object of type string?


Because string is a reference type, reference being null means
it does not have any object associated with it. string.Empty is a value
of a concrete object.
Jun 19 '06 #2
Hi,

string.Empty has a value, an empty string ( "" ) .

null is the absence of a value.

They are different things.

"Dylan Parry" <us****@dylanparry.com> wrote in message
news:52**************@dylanparry.com...
Hi folks,

I came across something strange a few moments ago when testing some
strings.

In my application a particular object has a name and a title, and is
displayed on a webpage thusly:

Object name - title

But the " - title" bit is only displayed, obviously, if it is
appropriate, ie. a title has been set for that object. I attempted to
achieve that by testing with:

if (myObject.Title != string.Empty) {
...
}

Yet this didn't work as the Title property was returning 'null'. So it
got me wondering, why is 'null != string.Empty' when the 'null' value
belongs to an object of type string?

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references

Jun 19 '06 #3
By the way, if you are using .NET 2.0, you can use the
String.IsNullOrEmpty() method instead of checking for NOT null && not empty

- José
"Dylan Parry" <us****@dylanparry.com> a écrit dans le message de news:
52**************@dylanparry.com...
Hi folks,

I came across something strange a few moments ago when testing some
strings.

In my application a particular object has a name and a title, and is
displayed on a webpage thusly:

Object name - title

But the " - title" bit is only displayed, obviously, if it is
appropriate, ie. a title has been set for that object. I attempted to
achieve that by testing with:

if (myObject.Title != string.Empty) {
...
}

Yet this didn't work as the Title property was returning 'null'. So it
got me wondering, why is 'null != string.Empty' when the 'null' value
belongs to an object of type string?

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references

Jun 19 '06 #4
Josòzoye wrote:
By the way, if you are using .NET 2.0, you can use the
String.IsNullOrEmpty() method instead of checking for NOT null && not empty


Oh that's good :) I didn't know about that one, but will do a quick
replace tomorrow on all the times I've done this so far.

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Jun 19 '06 #5
"Dylan Parry" <us****@dylanparry.com> wrote in message
news:5q***************@dylanparry.com...
Josòzoye wrote:
By the way, if you are using .NET 2.0, you can use the
String.IsNullOrEmpty() method instead of checking for NOT null && not
empty


Oh that's good :) I didn't know about that one, but will do a quick
replace tomorrow on all the times I've done this so far.


Another interesting thing is that VB.NET behaves different. The following
returns true in VB:
Dim b As Boolean = (String.Empty = Nothing)

If you want to distinguish between an empty string and null in VB.NET you
have to use String.Equals
I sometimes forget this when I switch between languages and this can lead to
some interesting bugs :-)

/claes
Jun 21 '06 #6
Claes Bergefall <lo*****@nospam.nospam> wrote:
"Dylan Parry" <us****@dylanparry.com> wrote in message
news:5q***************@dylanparry.com...
Josòzoye wrote:
By the way, if you are using .NET 2.0, you can use the
String.IsNullOrEmpty() method instead of checking for NOT null && not
empty


Oh that's good :) I didn't know about that one, but will do a quick
replace tomorrow on all the times I've done this so far.


Another interesting thing is that VB.NET behaves different. The following
returns true in VB:
Dim b As Boolean = (String.Empty = Nothing)

If you want to distinguish between an empty string and null in VB.NET you
have to use String.Equals
I sometimes forget this when I switch between languages and this can leadto
some interesting bugs :-)


You don't actually have to use String.Equals - you can use "Is":

Imports System

Class Test
Shared Sub Main()
Console.WriteLine (String.Empty = Nothing)
Console.WriteLine (String.Empty Is Nothing)
End Sub
End Class

Wacky, eh?

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

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

Similar topics

7
by: Andy Fish | last post by:
consider the following fragment of xsl: <xsl:variable name="v1"><xsl:if test="false()"/></xsl:variable> <xsl:variable name="v2"></xsl:variable> <xsl:value-of select="boolean($v1)"/>...
11
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
8
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...
13
by: santosh | last post by:
Hi, If I call free() with a uninitialised pointer, will the program state become undefined, or will free() return harmlessly? Incidentally, is there a way in Standard C to determine weather a...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
26
by: anonieko | last post by:
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.
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
19
by: Ulrich Eckhardt | last post by:
Greetings! I was recently surprised by the compiler's warning concerning this code: struct text { char* s; size_t len; }; int main() { struct text t = {"hello world!"};
4
by: Chronictank | last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.