C#'s char
type is aliasing the System.Char
type. It represents a Unicode character and occupies 2 bytes.
When we talk about characters we are referring to individual letters and numbers. For example, the letter 'a' is a character, as is the visual representation of the number '1
'. Such characters may be stored in a C# char variable. A char
variable can contain one character and one character only.
A char literal is specified inside single quotes (‘’
): char c = 'A';
For character data types we can specify literals in three ways:
We can specify literal to char data type as single character within single quote.
char ch = 'a';
We can specify char literals in Unicode representation ‘\uxxxx
’. Here xxxx
represents 4 hexadecimal numbers.
char ch = '\u0061';// Here /u0061 represent a.
Every escape character can be specified as char literals.
Escape sequences express characters that cannot be expressed 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 shown in the following table.
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 |
\u
(or \x
) escape sequence specify Unicode character using four-digit hexadecimal code:
char copyrightSymbol = '\u00A9';
char omegaSymbol = '\u03A9';
char newLine = '\u000A';
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// character literal within single quote
char ch = 'a';
// Unicode representation
char c = '\u0061';
Console.WriteLine(ch);
Console.WriteLine(c);
// Escape character literal
Console.WriteLine("Hello World\nFrom XDevSpace\t!");
Console.ReadLine();
}
}
}
Output:
Note that there is a special character sequence for the Backslash (\\
). Because the special characters begin with a backslash the compiler interprets any instances of a single backslash as the pre-cursor to a special character. This raises the question of what to do if you really want a backslash. The answer is to use the double backslash special constant sequence.
System.Char
defines a range of static methods for working with characters.
For example, You can use ToUpper
to convert a char to its upper case.
You can call these through either the System.Char
type or its char
alias:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(System.Char.ToUpper('c')); // C
Console.WriteLine(char.IsWhiteSpace('\t')); // True
Console.ReadLine();
}
}
}
ToUpper and ToLower honor the end user's locale.
System.Char
and System.String
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))
The following table lists static methods related to categorizing characters:
Static method | Characters included |
---|---|
IsLetter | A-Z , a-z , and letters of other alphabets |
IsUpper | Uppercase letters |
IsLower | Lowercase letters |
IsDigit | 0-9 plus digits of other alphabets |
IsLetterOrDigit | Letters plus digits |
IsNumber | All digits plus Unicode fractions and Roman numeral symbols |
IsSeparator | Space plus all Unicode separator characters |
IsWhiteSpace | All separators plus \n , \r , \t , \f , and \v |
IsPunctuation | Symbols used for punctuation in Western and other alphabets |
IsSymbol | Most other printable symbols |
Static method | Characters included |
IsControl | Nonprintable "control " characters below 0x20 , (None)such as \r , \n , \t , \0 , and characters between0x7F and 0x9A |
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.