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

0x0027

\”

Double quote

0x0022

\\

Backslash

0x005C

\0

Null

0x0000

\a

Alert

0x0007

\b

Backspace

0x0008

\f

Form feed

0x000C

\n

New line

0x000A

\r

Carriage return

0x000D

\t

Horizontal tab

0x0009

\v

Vertical tab

0x000B

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

stringis a reference type, not a value type. Its equality operators, however, implement value-type semantics. ...

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.