Austin T French
Austin T French

Reputation: 5131

Why am I getting the opposite of what is true in List<Char>.Contains(char)

I have tested this in two applications; Where I pass in a char looking for a special character.

        static void Main(string[] args)
        {
            ConsoleKeyInfo entered = Console.ReadKey();
            char asChar = Convert.ToChar(entered.KeyChar);
            Console.WriteLine(IsSpecialChar(asChar));

            Console.ReadKey();
        }

        private static string IsSpecialChar(char dWord)
        {
            List<char> Special = new List<char>();

            System.Globalization.NumberStyles hexer = System.Globalization.NumberStyles.HexNumber;
            Special.AddRange(Enumerable.Range((int.Parse("21", hexer)), int.Parse("2f", hexer)).Select(x => Convert.ToChar(x)));
            Special.AddRange(Enumerable.Range((int.Parse("3a", hexer)), int.Parse("40", hexer)).Select(x => Convert.ToChar(x)));
            Special.AddRange(Enumerable.Range((int.Parse("5c", hexer)), int.Parse("60", hexer)).Select(x => Convert.ToChar(x)));
            Special.AddRange(Enumerable.Range((int.Parse("7e", hexer)), int.Parse("7e", hexer)).Select(x => Convert.ToChar(x)));

            if (Special.Contains(Convert.ToChar(dWord)))
            {
                return "Special";
            }
            else return "NA";
        }

For some reason the Special.Contains always sees the opposite of what is true.

Some of the characters that are populated are:

U+003F  ?   3f  QUESTION MARK
U+0040  @   40  COMMERCIAL AT
U+0041  A   41  LATIN CAPITAL LETTER A
U+0042  B   42  LATIN CAPITAL LETTER B

Letters A and B should not be there. What is going on?

Upvotes: 0

Views: 79

Answers (1)

Servy
Servy

Reputation: 203811

Enumerable.Range accepts a start of the range and the length of the range rather than a start of the range and an end of the range.

Because of this, your ranges aren't at all what you intended them to be.

Upvotes: 7

Related Questions