Many of the core facilities that you need when programming are provided not by the C# language, but by types in the .NET Framework. In this chapter, we cover the Framework’s role in fundamental programming tasks, such as virtual equality comparison, order comparison, and type conversion. We also cover the basic Framework types, such as String
, DateTime
, and Enum
.
The types in this section reside in the System
namespace, with the following exceptions:
StringBuilder
is defined in System.Text
, as are the types for text encodings.
CultureInfo
and associated types are defined in System.Globalization
.
A C# char
represents a single Unicode character and aliases the System.Char
struct. In Chapter 2, we described how to express char
literals. For example:
char c = 'A'; char newLine = ' ';
System.Char
defines a range of static methods for working with characters, such as ToUpper
, ToLower
, and IsWhiteSpace
. You can call these through either the System.Char
type or its char
alias:
Console.WriteLine (System.Char.ToUpper ('c')); // C Console.WriteLine (char.IsWhiteSpace (' ')); // True
ToUpper
and ToLower
honor the end user’s locale, which can lead to subtle bugs. The following expression evaluates to false
in Turkey:
char.ToUpper ('i') == 'I'
because in Turkey, char.ToUpper ('i')
is 'İ'
(notice the dot on top!). To avoid this problem, System.Char
(and System.String
) also provides culture-invariant versions of ToUpper
and ToLower
ending with the word Invariant. These always apply English culture rules:
Console.WriteLine (char.ToUpperInvariant ('i')); // I
This is a shortcut for:
Console.WriteLine (char.ToUpper ('i', CultureInfo.InvariantCulture))
For more on locales and culture, see “Formatting and parsing”.
Most of char
’s remaining static methods are related to categorizing characters and are listed in Table 6-1.
Static method | Characters included | Unicode categories included |
---|---|---|
IsLetter |
A–Z, a–z, and letters of other alphabets | UpperCaseLetter LowerCaseLetter TitleCaseLetter ModifierLetter OtherLetter |
IsUpper |
Uppercase letters | UpperCaseLetter |
IsLower |
Lowercase letters | LowerCaseLetter |
IsDigit |
0–9 plus digits of other alphabets | DecimalDigitNumber |
IsLetterOrDigit |
Letters plus digits | (IsLetter , IsDigit ) |
IsNumber |
All digits plus Unicode fractions and Roman numeral symbols | DecimalDigitNumber LetterNumber OtherNumber |
IsSeparator |
Space plus all Unicode separator characters | LineSeparator ParagraphSeparator |
IsWhiteSpace |
All separators plus
,
, , f , and v |
LineSeparator ParagraphSeparator |
IsPunctuation |
Symbols used for punctuation in Western and other alphabets | DashPunctuation ConnectorPunctuation InitialQuotePunctuation FinalQuotePunctuation |
IsSymbol |
Most other printable symbols | MathSymbol ModifierSymbol OtherSymbol |
IsControl |
Nonprintable “control” characters below 0x20, such as
,
, ,
|