TOC
Data types:

The Char type

The System.Char data type is used to hold a single, unicode character. C# has an alias for it, called char, which you can use when declaring your char variables:

char ch;

And of course, you can immediately assign a value to it, if you want to. In C#, a char is surrounded by a set of single quotes:

char ch = 'H';

Since a string (which we'll discuss in the next chapter) is basically just a range of characters, .NET actually uses a list of char's to represent a string. That also means that you can pull out a single char from a string, or iterate over a string and get each character as a char data type:

string helloWorld = "Hello, world!";
foreach(char c in helloWorld)
{
    Console.WriteLine(c);
}

Under the helmet, a char is a numerical value, where each character has a specific number in the Unicode "alphabet". As of writing, there are more than 130.000 different Unicode characters, ranging from the Latin/western alphabet to historical scripts. So, in C#, you can very easily go from a char data type to its numeric representation, as illustrated by this slightly extended version of the previous example:

string helloWorld = "Hello, world!";
foreach(char c in helloWorld)
{
    Console.WriteLine(c + ": " + (int)c);
}

It will simply output the character, followed by the numerical representation, simply by casting the char to an integer. This also means that you can just as easily go the other way around: From a number to a character. But why would you do that? Well, there are a lot of characters which are not directly available on most keyboards, e.g. the copyright (©) character. You can instead use a Unicode lookup table, find the numeric version of the character you need, and then just make it into a char:

char ch = (char)169;
Console.WriteLine(ch);

Char helper methods

The Char class has some really cool helper methods, which can help you determine the type of char you're currently dealing with. This is very useful in a lot of situations, e.g. when validating input. Here's an example:

Console.WriteLine("Enter a single number:");
char ch = Console.ReadKey(true).KeyChar;
if (Char.IsDigit(ch))
    Console.WriteLine("Thank you!");
else
    Console.WriteLine("Wrong - please try again!");

I simply read the first key pressed by the user and then use the Char.IsDigit() method to see if it's a number or not. And there are many methods like this one, to check the type of the character. We can use this to do some very simple string validation:

Console.WriteLine("Write your name:");
string name = Console.ReadLine();  
bool isValid = true;  
for(int i = 0; i < name.Length; i++)  
{  
    char ch = name[i];  
    if((i == 0) && ((!Char.IsLetter(ch)) || (!Char.IsUpper(ch))))  
    {  
        Console.WriteLine("The first character has to be an uppercase letter!");  
        isValid = false;  
        break;  
    }  
    if(Char.IsDigit(ch))  
    {  
        Console.WriteLine("No digits allowed!");  
        isValid = false;  
        break;  
    }  
}  
if (isValid)  
    Console.WriteLine("Hello, " + name);

We simply loop through the name entered by the user and use various versions of the Is* methods to check that the input matches our simple requirements. And there are several other useful methods, like the Char.IsLetterOrDigit() method. For a full list, have a look at the documentation.

Summary

A char data type (alias for System.Char) is used to represent a single Unicode character. To represent more than one char, you use a string, which is basically just a list of chars. In the next article, we'll discuss strings.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!