Link to home
Start Free TrialLog in
Avatar of Khel4Me
Khel4MeFlag for India

asked on

String.Empty in different OS (Vista, Windows Server 2003)

Hi!
I am uing Visual Studio 2008 Profession Ediion.

The code I have attached shows/return True in Windows Vista but in Windows 2003 Server machine same code return False.
Why so?

If I use String.IsNullOrEmpty(s) it return True in both OS and working.
What is the best way to check for empty string.
1. String.IsNullOrEmpty(s)
2. s.Length
3. or any other method.

Thanks,
Amit

Dim s As String = ""
        Console.WriteLine(s Is String.Empty)
        Console.ReadLine()

Open in new window

Avatar of hongjun
hongjun
Flag of Singapore image

Personally, I feel String.IsNullOrEmpty(s) is preferred since it check for empty string and null
Avatar of Khel4Me

ASKER

I want to know why there is different results for
(s Is String.Empty)
Amit
It could be the s is null instead of empty.
Avatar of Khel4Me

ASKER

Hongjun:
Take a look at code snippet I have set the variable value to "", so there is no chance for Nothing or NULL
Amit
Use this instead
Console.WriteLine(s = String.Empty)

Open in new window

Or this
Console.WriteLine(s.Equals(String.Empty))

Open in new window

If you use this, you will get what you want.
Reason is because of a slight difference between String.Empty and ""

http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx
Dim s As String = String.Empty
Console.WriteLine(s Is String.Empty)

Open in new window

Avatar of Khel4Me

ASKER

hongjun:
I want to know why the attached code returns / displays True when run on Vista machine but False when run in Windows 2003 Server?
Amit

Dim s As String = String.Empty 
Console.WriteLine(s Is String.Empty) 

Open in new window

Hi Amit,

You are probably aware of the fact that "Is" compares two objects and should not be used with strings (but hongjun already told you that). The result, however, should be equal on whatever system you are running your code and, iirc, should always be false.

The reason for that is that the String object you are creating is a new object (this is typical for string objects) and is not pointing to the same object (the static String.Empty). Somehow, it seems that the IL compiler screws up while optimizing and replaces the "s" variable with a constant instead of keeping it a changeable object.

You may receive better results if you "use" the string object. I.e., if you assign another value to it, and later the empty value, to force to compiler not to optimize.

Be as it may, the way you put it, this is a bug in your version of .NET (which version is that exactly, did you update to .NET 3.5 SP1?).

-- Abel --
And the reason as to why Win Vista and 2003 behave differently, I have no idea.
It could be the way how the individual OS handles objects.

hongjun
Since .NET is platform agnostic and since the CLR is based on well-defined open standards (and so it the IL), the code has to work the same way on every system where you run it, provided the systems have the same version of the CLR. The OS has nothing to do with how it handles objects, because it doesn't, the language and the CLR define how objects are being dealt with.

Can you check the version of the .NET runtime on the two machines?
abel is correct.
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Khel4Me

ASKER

Increasing points
tx, that's nice of you :)