String Literals

  • C# string literals includes regular string literals and verbatim string literals.
  • A regular string literal is a combination of zero or more characters in a pair of double quotes.
  • A character in a regular string literal can be a simple escape sequence, a hexadecimal escape sequence or an unicode escape sequence.
  • A verbatim string literal starts with @ character followed by zero or more characters in double quotes.
  • Only double quote escape sequence "" is supported in a verbatim string literal.
  • A simple escape sequence, a hexadecimal escape sequence or an unicode escape sequence are not processed in a verbatim string literal.
  • A verbatim string literal may span multiple lines.

The following example will demonstrate the difference between regular and verbatim string literals.

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
using System;

namespace StringLiterals1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1, s2;

            s1 = "Good\tmorning\nbuddy.";
            s2 = @"Good\tmorning\nbuddy.";
            Console.WriteLine(s1);
            Console.WriteLine(s2);

            s1 = "1: Hello \"World\"";
            //s2 = @"Hello \"World\"";
            Console.WriteLine(s1);

            //s1 = "Hello ""World""";
            s2 = @"2: Hello ""World""";
            Console.WriteLine(s2);

            s1 = "\\\\server\\shares\\shared_file.txt";
            s2 = @"\\server\shares\shared_file.txt";
            Console.WriteLine(s1);
            Console.WriteLine(s2);

            s2 = @"Have
a
nice
day";
            Console.WriteLine(s2);

            Console.Read();

        }
    }
}

Output

Good	morning
buddy.
Good\tmorning\nbuddy.
1: Hello "World"
2: Hello "World"
\\server\shares\shared_file.txt
\\server\shares\shared_file.txt
Have
                   a
                   nice
                 day
  • Line 11-14: Output two string s1 is a regular string and s2 is a verbatim string. Apparent \t and \n in s2 have no effect on the result because of verbatim string not processing simple escape sequence.
  • Line 16-18: If the comment double slashes were removed in line 17, you would get a compile time error. Because \" is not supported in a verbatim string in line 17 but it is supported in line 16.
  • Line 20-22: If you want to output double quote, you will have to use "" instead of \" in a verbatim string. Check line 21, s2 is defined well. Line 20 was commented out because "" is not supported in regular string.
  • Line 24-27: This is a good example how to output a backslash. In line 24, \\ is used in a regular string and in line 25 the file path is just defined as it was original. So when defining a file path, a verbatim string is better than a regular string.
  • Line 29-33: A verbatim string is defined in multiple lines.

When two strings with the same value are assigned to 2 string variables separately. If they are in the same code context or assembly, the second string literal will not be created. They will share the same content in memory. Check the example 01-21-02 below.

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

namespace StringLiterals2
{
    class Program
    {
        static void Main(string[] args)
        {
            string first = "How are you?";
            string second = "How are you?";

            Console.WriteLine(first == second ? "Same" : "Not same");

            Console.Read();
        }
    }
}

Output

Same
  • Line 9-10: Two variables are defined with initializing the same literal "How are you?". Based on what we learned in C# data types, string belongs to reference type and its content is saved in the heap.
  • Line 12: We are using ? to judge if first and second string are equal. In line 10, when second variable is initialized. The system will search the heap and find the string literal was already existed. The its address or location is returned and assigned to second variable. That means the two variables hold the same value - the literal address. So the condition returns true. "Same" is then outputted.