Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

list

indexof

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

like image 388
Jared Price Avatar asked Nov 10 '22 11:11

Jared Price


1 Answers

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.

like image 136
Jared Price Avatar answered Nov 15 '22 11:11

Jared Price