#25 – String Literals

A string literal in C# represents a sequence of Unicode characters.  There are two main types of string literals in C#–regular string literals and verbatim string literals.  Verbatim string literals allow including special characters in a string directly, rather than having to specify them using an escape sequence.  All string literals are of type string.

Here are some examples of string literals:

 string s1 = "Hi";
 string s2 = @"Hi";       // Verbatim string literal--same thing
 string s3 = "C:\\Dir";   // C:\Dir  (escape seq for backslash)
 string s4 = @"C:\Dir";   // No escape seq required
 string s5 = "\x48\x69";  // Hi  (hex codes for each character)
 string s6 = "\x20AC 1.99";  // € 1.99
 string s7 = "€ 1.99";    // Unicode directly in string

 // UTF-32 characters using surrogate pairs
 string s8 = "\U00020213";      // U+20213 (UTF-32)
 string s9 = "\ud840\ude13";    // Equiv surrogate pair
 string s10 = "𠈓";             // Same character

Note:

  • \u is followed by 4-byte UTF16 character code
  • \U is followed by 8-byte UTF32 character code, which is then converted to equivalent surrogate pair

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers