Strings and Characters
C#’s char
type (aliasing the System.Char
type) represents a Unicode character, and it
occupies two bytes. A char
literal
is specified inside single quotes:
char c = 'A'; // simple character
Escape sequencesexpress characters that cannot be expressed or interpreted literally. An escape sequence is a backslash followed by a character with a special meaning. For example:
char newLine = '\n'; char backSlash = '\\';
The escape sequence characters are outlined below.
Char |
Meaning |
Value |
---|---|---|
\' |
Single quote |
|
\” |
Double quote |
|
\\ |
Backslash |
|
|
Null |
|
|
Alert |
|
|
Backspace |
|
|
Form feed |
|
|
New line |
|
|
Carriage return |
|
|
Horizontal tab |
|
|
Vertical tab |
|
The \u
(or \x
)
escape
sequence lets you specify any Unicode character via its four-digit hexadecimal code:
char copyrightSymbol = '\u00A9'; char omegaSymbol = '\u03A9'; char newLine = '\u000A';
Char Conversions
An implicit conversion from a char
to a numeric
type works for the numeric
types that can accommodate an unsigned short. For other numeric types, an explicit
conversion is required.
String Type
C#’s string type (aliasing the System.String
type) represents an immutable sequence of Unicode characters. A
string literal is specified inside double quotes:
string a = "Heat";
Note
string
is a reference type, not a value type. Its equality
operators, however, implement value-type semantics.
The ...
Get C# 3.0 Pocket Reference, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.