Character Literal (const char)Examine character literals, which use single-quotes. Review escape sequences.
This page was last reviewed on May 20, 2023.
Character literal. A character literal is a value. It is usually assigned to a variable of type char. Character literals in the C# language are enclosed in single-quote characters.
In character literals, some values must be escaped with backslash characters. This can lead to compile-time errors if not done correctly. Care must be used.
First, we use character literals in the C# language. Char variables, such as those at the class level or the local variable level, are assigned to the character literal values.
Info The literals are immutable and cannot be changed, but the variables that store those values simply denote storage locations.
Here The const char in the program uses value semantics and cannot be reassigned. It is a named literal.
String Literal
Detail Character literals are escaped with the "\" character, and this is how you can represent the newline "\n".
Finally We print the literal values to the screen with Console.WriteLine—the backslash is no longer present.
using System; class Program { static char _literal5 = 'e'; // Static character literal. const char _literal6 = '#'; // Constant character literal. static void Main() { // // Shows some example character literals. // ... Also shows a local constant character literal. // ... Then prints them. // char literal1 = 'A'; char literal2 = 'b'; char literal3 = '\\'; const char literal4 = '_'; Console.WriteLine(literal1); Console.WriteLine(literal2); Console.WriteLine(literal3); Console.WriteLine(literal4); Console.WriteLine(_literal5); Console.WriteLine(_literal6); } }
A b \ _ e #
Compile-time errors. It is critical to understand how char literal escapes work. This program shows some possible errors in a C# program.
And For a newcomer to C# this program could be a challenge to debug (we need double backslashes in the literal).
class Program { static void Main() { char backslash = '\'; } }
Error CS1010 Newline in constant Error CS1012 Too many characters in character literal Error CS1002 ; expected
A discussion. A variable (like char) denotes a storage location. This can be reassigned to different values. A character literal denotes a value, and this can never be changed in memory.
Further A const char can only be used as a value, not a variable. It is a way of referring to a character literal by a name.
A summary. Character literals represent character values. They are a form of value representation, not a variable type—they are constants.
Character literals cannot be changed during runtime, but the variables that store them can. We must escape some literals, like newlines and the backslash.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.