c# – Too many characters in character literal error

c# – Too many characters in character literal error

This is because, in C#, single quotes () denote (or encapsulate) a single character, whereas double quotes () are used for a string of characters. For example:

var myChar = =;

var myString = ==;

Heres an example:

char myChar = |;
string myString = ||;

Chars are delimited by single quotes, and strings by double quotes.

The good news is C# switch statements work with strings!

switch (mytoken)
{
    case ==:
        //Something here.
        break;
    default:
        //Handle when no token is found.
        break;
}

c# – Too many characters in character literal error

You cannot treat == or || as chars, since they are not chars, but a sequence of chars.

You could make your switch…case work on strings instead.

Leave a Reply

Your email address will not be published.