Using the char Type

In C#, the char is a built-in value type that represents one character in a string. The character can be accessed by indexing through the string and converting to an integer. Let’s try it out by trying to access the letter (or char) ’t’ in the string “autumn”

using System;
class Program
{
    static void Main()
    {
       char a = "autumn"[2];
       if (a == 't')
    {
       Console.WriteLine(true);
    }
}
}

So in the above example, we index through the string “autumn” to find the letter t, which is at index value 2. Then we test the char using an if statement. Because “autumn” 2 = ’t’ (make sure your char is within single quotes), then the output of this if statement will be True. The char value type is rarely used in this exact context, but playing around with chars in this way will help you understand in general the purpose of the char type. The char value type can be particularly useful to you if you need to compare different characters, or if you’re trying to convert a string to individual characters. The char value type is probably most commonly used to index through an array of individual characters to access, print, or store particular characters.