indexOf question

  • Thread starter Thread starter raulavi
  • Start date Start date
R

raulavi

vs2005 c#
string var = string.Empty;
why "76090,76091,76092,G0202,G0204,G0206".IndexOf(var)
returns 0 ?

should be -1 for not found...empty is not part of my string being searched
any ideas
thanks
 
Hi,

In case you want to test for an empty string use

if(string.IsNullOrEmpty("Hello World"))
{
// string is either null or ""
}
 
Morten Wennevik said:
Hi,

In case you want to test for an empty string use

if(string.IsNullOrEmpty("Hello World"))
{
// string is either null or ""
}

I would be hard pressed to use IsNullOrEmpty to check for an empty string.
This is due to the fact that Null means the absence of a value while an
empty string is a string whose value contains no characters (hence empty
string). IsNullOrEmpty is the same as:

("Hello World" == null || "Hello World" == string.Empty)

while checking for an empty string is:

("Hello World" == string.Empty)

Therefore, they are not functionally equivalent. Although, I would usually
use IsNullOrEmpty in place of just an empty string check in *most* cases.
Some cases, you may not want to check for null, maybe you need to do
something different for null strings (raise an exception?) compared to empty
strings (allowed so no exception?).

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top