blah blah blah is here! blah blah » Close

up0down
link

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

last answered 2 years ago

1 answers

up0down
link

When a string variable is set to null, this means that it doesn't currently refer to a string instance.
An empty string is a string instance which contains no characters. In other words its Length property is 0.

up0down
link

Just as a tip, there is also a handly method that I just personally started using in my code:
String.IsNullOrEmpty();
Before using this handly method I was always doing things like:
if(null != id && 0 != id.length)
{
}
Now I can just do:
if(!String.IsNullOrEmpty(id))
{
}

up0down
link

Thanks dotnetprofessor,
I like this API.
regards,
George

up0down
link

Thanks,
you saved a lot of time.

up0down
link

Cool, vulpes!
regards,
George

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback