CSharp - String String Type

Introduction

C#'s string type is aliasing the System.String type.

string type represents an immutable sequence of Unicode characters.

A string literal is specified inside double quotes:

string a = "Heat";

string is a reference type. Its equality operators, however, follow value-type semantics:

string a = "test";
string b = "test";
Console.Write (a == b);  // True

The escape sequences that are valid for char literals also work inside strings:

string a = "Here's a tab:\t";

Verbatim string literals

C# supports verbatim string literals.

A verbatim string literal is prefixed with @ and does not support escape sequences.

The following code shows how to use verbatim string.

string a2 = @"\\server\fileshare\helloworld.cs";

A verbatim string literal can also span multiple lines:

string escaped  = "First Line\r\nSecond Line";
string verbatim = @"First Line
Second Line";

// True if your IDE uses CR-LF line separators:
Console.WriteLine (escaped == verbatim);

To include the double-quote character in a verbatim literal, write double quote twice:

string xml = @"<customer id=""123""></customer>";

Exercise