Re: why does IndexOf still return -1
- From: "Nick Hounsome" <nh002@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 03:45:33 GMT
"Gordon Cowie" <gordy@xxxxxxxxxxxxxxxxxx> wrote in message
news:OHu6GY1UGHA.5900@xxxxxxxxxxxxxxxxxxxxxxx
No because then if you need the index if it is there but it is optional
(a common case) then you must either incur the exception cost or incur
the search cost twice.
I see. Also, from reading other's replies. The real issue is the "cost"
related to exceptions. If they were free I think it would make for a much
cleaner coding style. Probably even anywhere a special return code is
returned.
try
{
x = s.IndexOf('x');
}
catch( Exception e )
{
// how is it clear that this relates to the indexing?
}
v
x = s.IndexOf('x');
if( x < 0 )
{
}
else
{
}
Worse yet you just know that you are going to find a load of code like:
try
{
x = s.IndexOf('x');
}
catch {}
Another negative about -1. It forces IndexOf to return an int, when a uint
would be more appropriate. The max length of the string is effectively cut
in half in order to provide space for the possible -1 result.
uint is not CLSCompliant so it's never going to happen.
If you have strings of over 2GB then indexing will be the least of your
problems.
P.S. In my opinion they should at least have provided
public const int NotFound = -1;
I'm not a fan of magic numbers even for error returns.
.
- References:
- why does IndexOf still return -1
- From: Gordon Cowie
- Re: why does IndexOf still return -1
- From: Nick Hounsome
- Re: why does IndexOf still return -1
- From: Gordon Cowie
- why does IndexOf still return -1
- Prev by Date: Re: Updating Programs
- Next by Date: Re: Error In Command Window - Method does not exist
- Previous by thread: Re: why does IndexOf still return -1
- Next by thread: Re: why does IndexOf still return -1
- Index(es):
Relevant Pages
|