Characters are the fundamental building blocks of C# source code. Every program is composed of characters that, when grouped together meaningfully, create a sequence that the compiler interprets as instructions describing how to accomplish a task. A program also can contain character constants. A character constant is a character that’s represented as an integer value, called a character code. For example, the integer value 122
corresponds to the character constant 'z'
. The integer value 10
corresponds to the newline character '
'
. Character constants are established according to the Unicode character set, an international character set that contains many more symbols and letters than does the ASCII character set (listed in Appendix C). To learn more about Unicode, see our online appendix.
A string is a series of characters treated as a unit. These characters can be uppercase letters, lowercase letters, digits and various special characters: +
, -
, *
, /
, $
and others. A string is an object of type string
. Just as the C# keyword object
is an alias for class Object
, string
is an alias for class String
(namespace System
). We write string literals, also called string constants, as sequences of characters in double quotation marks, as follows:
"John Q. Doe"
"9999 Main Street"
"Waltham, Massachusetts"
"(201) 555-1212"
A declaration can assign a string
literal to a string
reference. The declaration
string color = "blue";
initializes the string
color
to refer to the string
literal object "blue"
.
If there are multiple occurrences of the same string
literal object in an app, a single copy of it will be referenced from each location in the program that uses that string
literal. It’s possible to share the object in this manner, because string
literal objects are implicitly constant. Such sharing conserves memory.
strings
Recall that backslash characters in string
s introduce escape sequences and that placing a literal backslash in a string
requires \
. On occasion, a string
will contain multiple literal backslash characters (this often occurs in the name of a file). To avoid excessive backslashes, it’s possible to exclude escape sequences and interpret all the characters in a string
literally, using the @
character to create what’s known as a verbatim string. Backslashes within the double quotation marks following the @
character are not considered to be part of escape sequences. Often this simplifies programming and makes the code easier to read.
For example, consider the string
in the following assignment:
string file = "C:\MyFolder\MySubFolder\MyFile.txt";
Using the verbatim string
syntax, the assignment can be altered to
string file = @"C:MyFolderMySubFolderMyFile.txt";
Verbatim string
s may also span multiple lines, in which case they preserve all newlines, spaces and tabs between the opening @"
and closing "
delimiters.