The char Type
A char holds exactly one letter, digit, or symbol, like a single tile in a word game. You write it with single quotes.
In this page:
Syntax
char variableName = 'A';
int code = (int)variableName;
bool isDigit = char.IsDigit(variableName);
The char Type
A char represents a single UTF-16 character and is written with single quotes, like A. Characters can be compared, cast to their underlying numeric code point, and combined into strings. Double quotes are reserved for string, not char.
Note:
Casting a
char to int gives you its Unicode code point.Example: The char Type
using System;
class Program
{
static void Main(string[] args)
{
char letter = 'A';
char digit = '7';
Console.WriteLine(letter);
Console.WriteLine((int)letter);
Console.WriteLine(char.IsDigit(digit));
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using double quotes for a
char, such aschar c = "A";, when a char needs single quotesA. - Expecting
a + 1to be a char, when the result is anintand needs a cast tochar. - Trying to put more than one character in a
charliteral likeab.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: