The <string> object IndexOf list returns -1. How?

It really bothers me. Somehow, the IndexOf(string) method of my List<string> object returns -1, even if the CLEARLY value is in the list.

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

string prefix - "01"

enter image description here

validPrefixes index 0 is "01".

enter image description here

indexOf is -1. Umm ... How?

enter image description here

+5
source share
1 answer

Well, I'm not sure why, but it looks like my debugger was lying to me. I believe that everything that was in validPrefixes was lower for everyone, since I used the ToLower() method earlier in my code that produced the string addr variable.

For some reason, my debugger was somehow mixing loop iterations. I could only say that this happened by assigning char[] variables using the string.ToCharArray() method.

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

Somehow, the debugger told me that the value of prefix was "01" , and the value of prefix.ToCharArray() was ['f','e'] . The latter was what the value should have been from another iteration of the loop.

Since I checked "FE" instead of "FE" , my code did not work, but my debugger didnโ€™t tell me about it.

I am developing for iOS using Xamarin on my Windows PC using the XAMAIN iOS build host. In the past, I had strange things developing like this, especially when it comes to debugging and breakpoints. Therefore, I will blame the Xamarin Build Host.

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

+1
source

All Articles