List<string> object IndexOf returning -1. How?

621 views Asked by At

This is very baffling to me. Somehow the IndexOf(string) method of my List<string> object is returning -1 even though the value is CLEARLY in the List.

protected readonly List<string> validPrefixes = new List<string>() { "01", "02", "03", "FE", "E1", "E2", "E3" };

string prefix is "01"

enter image description here

validPrefixes index 0 is "01".

enter image description here

indexOf is -1. Umm... How?

enter image description here

1

There are 1 answers

3
Jared Price On BEST ANSWER

Well I'm not sure why, but it looks like my debugger was lying to me. I believe what was happening was the values in validPrefixes needed to all be lower case since I was using the ToLower() method previously in my code which effected the string addr variable.

For some reason my debugger seemed to somehow be mixing iterations of a loop. I was only able to tell this was happening by assigning variables to a char[] using the string.ToCharArray() method.

("01").ToCharArray() was returning ['0','1']. prefix.ToCharArra() was returning ['f','e'].

Somehow, the debugger was telling me the value of prefix was "01" while the value of prefix.ToCharArray() was ['f','e']. The latter being what the value should have been from a different loop iteration.

Since I was checking for "FE" instead of "fe", my code wasn't working, but my debugger wasn't exactly telling me this.

I am developing for iOS using Xamarin on my Windows PC using the Xamarin iOS Build Host. I have had weird things happen to me in the past by developing like this especially when it comes to debugging and breakpoints. So I'm going to blame the Xamarin Build Host.

Thanks to @Amit and @DavidW for helping me come to this conclusion.