Hello everyone,
I am doing input parameter checking for a string type, and string value except empty is valid. My function is provided to outer client to call. Sometimes I find to check null is not enough, I also need to check String.Empty.
My question is, what are the differences between null and String.Empty? I have made some web search for the answers but not very useful information.
thanks in advance,
George
George GeorgePosted Jun 23, 2008, 5:59 AM
Thanks Brandon,
Great reply! Two more comments,
1.
In your pseudo code of Empty property, each time a new instance is returned, right? So, references to String.Empty are pointing to different memory locations, right?
2.
"And since the property is a read-only property, there is no chance of modifying it and messing anything else up in the application." -- if the property is not read-only, what actions you could do to analyze?
regards,
George
George GeorgePosted Jun 23, 2008, 5:50 AM
Hi Dave,
I am interested in your solution -- "With 3.5, you can declare and use pointers (just like C++) inside an unsafe block. Theoretically, initialise a couple of pointers to strings which are initialised to String.Empty." Could you show your code please? So that all of us could learn from you.
regards,
George
Posted Jun 21, 2008, 1:23 AM
DavePosted Jun 21, 2008, 12:24 AM
With 3.5, you can declare and use pointers (just like C++) inside an unsafe block. Theoretically, initialise a couple of pointers to strings which are initialised to String.Empty. Then send those addresses to output.
I'm still on 2.0. So I cannot help.
Posted Jun 20, 2008, 4:18 PM
Of course the property could also look likes something crazy, like this:
public unsafe static string Empty
{
get
{
char tempChar = ' ';
return new string(&tempChar);
}
}
Its hard to say what goes on in there... this is why Im going back and learning C++ so I can get a better understanding of the C# language.
And since the property is a read-only property, there is no chance of modifying it and messing anything else up in the application.
George GeorgePosted Jun 20, 2008, 5:10 AM
Thanks Brandon!
Great! I found Empty is a static member of String class, so, it means all string type variables initialized with String.Empty are pointing to the same thing?
regards,
George
Posted Jun 19, 2008, 2:24 PM
string myString; //Without initializing it, will create a null string.
An empty string is a string that has been initialized and given some memory, but it just doesn't contain any characters (except a null terminator at the end, but you don't see that) so as far as the compiler and you are concerned, it is a string with a length of 0.
string myString = String.Empty; //Will create an empty string.
If you want to check for both a null and empty string, use the static method proved by the String class:
if (String.IsNullOrEmpty(myString))
{
//do something
}