Character Literals

  • A character literal stands for a character in a pair of single quotations. For example, 'B', 't' or '?', etc.
  • The character can be a simple escape sequence. For example, '\\', '\'' or '\n'.
  • The simple escape sequence starts with a \ and following one of the characters ', ", \, 0, a, b, f, n, r, t, u, U, x, v.
  • The character can be an unicode character escape sequence.
  • The unicode character escape sequence starts with \u and the following are 4 hex digits or \U and the following 8 hex digits.
  • In C#, each character is presented by a 16-bit encoding of Unicode code so a character literal must be presented in the range of from \u0000 to \uFFFF. For example, \u0041 stands for A.
  • If we use \x to show a character literal, it will be called hexadecimal escape sequence and the following are 4 hex digits. The literal is also in the range of from \x0000 to \xFFFF. For example, \x0041 is A.

The following is the simple escape sequence table.

Escape SequenceDescriptionUnicodeExamples
\'Single Quote0x0027char c = '\'', u='\x0027';
\"Double Quote0x0022char c = '\"', u='\x0022';
\\Backslash0x005Cchar c = '\\', u='\x005C';
\0Null character0x0000char c = '\0', u='\u0000';
\aAlert or beep0x0007char c = '\a', u='\u0007';
\bBackspace0x0008char c = '\b', u='\x0008';
\fForm feed0x000Cchar c = '\f', u='\x000C';
\nNew line0x000Achar c = '\n', u='\x000A';
\rCarriage return0x000Dchar c = '\r', u='\x000D';
\tHorizontal tab0x0009char c = '\t', u='\u0009';
\vVertical tab0x000Bchar c = '\v', u='\u000B';

Example 01-20-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
33
33
34
35
36
37
38
39
40
using System;

namespace SimpleEscape
{
    class Program
    {
        static void Main(string[] args)
        {
            string s;

            s = "\"abc\'d\'efg\"";
            Console.WriteLine(s);

            s = "abcd\\\tefg";
            Console.WriteLine(s);

            s = "abcd\aefg";
            Console.WriteLine(s);
            Console.WriteLine(s.Replace('\a','z'));

            s = "abcd\0efg";
            Console.WriteLine(s);
            Console.WriteLine(s.Replace('\0','z'));

            s = "abcd\nefg";
            Console.WriteLine(s);
            Console.WriteLine(s.Replace('\n','z'));

            s = "abcd\refg";
            Console.WriteLine(s);
            Console.WriteLine(s.Replace('\r','z'));

            s = "abcd\befg";
            Console.WriteLine(s);
            Console.WriteLine(s.Replace('\b','z'));

            Console.Read();
        }
    }
}

Output

"abc'd'efg"
abcd\	efg
abcdefg
abcd efg
abcdzefg
abcd
efg
abcd
efg
abcefg
  • We know a string is a combination of characters. The above example will show you how simple escape sequence works by outputting strings.
  • Line 11-12: Output single quotes and double quotes.
  • Line 14-15: Output a backslash and a horizontal tab.
  • Line 17-18: You should hear a beep when printing \a character.But you cannot find the character in the string.
  • Line 19: Replace method is used to replace the first parameter with second one in the string. So in this statement, the '\a' was replaced with 'z' and then output the result. You can see 'z' is inside the string. That means '\a' is still in string s even if it cannot be shown up when outputting.
  • Line 21-23: Output \0 character and then is replaced with z and output z.
  • Line 25-27: Output a new line to make the string displayed in 2 lines.
  • Line 29-31: Output \r character. It makes the cursor go to the first column and overwrite previous output. But the character is still in the string. You can see z is outputted after it replaced \r.
  • Line 33-35: Output a backspace inside string. The character d was removed by the backspace character. But you can see the character location after it was replaced by z.

The difference between two of them is demonstrated in the example below.

Example 01-20-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace UnicodeEscape
{
    class Program
    {
        static void Main(string[] args)
        {
            char \u0061 = '\x0061';
            bool b = true;

            Console.WriteLine("a={0}", a);

            Console.WriteLine(\u0062 ? "b is true." : "b is false.");

            \u0043onsole.Read();
        }
    }
}

Output

a=a
b is true.
  • Line 9: This is amazing. \u0061 is the unicode of character a. So this line defines a char variable a. And its value is initialized as 'a'.
  • Line 10: Define a boolean variable b and assigned it with true.
  • Line 12: Output variable a, you will see what I said in line 9.
  • Line 14: Apparently \u0062 is b. Because b was assigned as true. The output is "b is true.".
  • Line 16: \u0043 is C. So the line is "Console.Read()".
  • Line 9, 14: If \u0061 was replaced with \x0061 in line 9 or \u0062 was replaced with \x0062, you would get a compile time error. That's the difference between an unicode character and a hexadecimal character.
  • A hexadecimal character can only be shown in a literal but the unicode of a character can replace the character anywhere in the program.

The above example 01-20-02 is equivalent to the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace UnicodeEscape
{
    class Program
    {
        static void Main(string[] args)
        {
            char a = 'a';
            bool b = true;

            Console.WriteLine("a={0}", a);

            Console.WriteLine(b ? "b is true." : "b is false.");

            Console.Read();
        }
    }
}