"Too many characters in character literal error"
I'm struggling with a piece of code and getting the error:
Too many characters in character literal error
Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line:
case '&&':
case '||':
case '==':
How can I keep the ==
and &&
as a char?
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 = "==";
Here's 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;
}
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.