๐ŸŽ“ A1Learner
๐Ÿ”

C# Characters

A char is a type that holds one single character. A char value sits in single quotes, like 'A'.

One letter

A char variable holds one letter here. Printing the variable shows the letter itself.

using System;

class Program
{
    static void Main(string[] args)
    {
        char grade = 'B';
        Console.WriteLine(grade);
    }
}

A char plus one

A char carries a number code behind the letter. Adding 1 to A gives the int 66, and the program prints that number.

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine('A' + 1);
    }
}

Spelling with Write

Console.Write prints and stays on the same line. Two Write calls place the letters side by side.

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.Write('H');
        Console.Write('i');
    }
}

Example

using System;

class Program
{
    static void Main(string[] args)
    {
        char mathGrade = 'A';
        char artGrade = 'B';
        Console.WriteLine("Math: " + mathGrade);
        Console.WriteLine("Art: " + artGrade);
    }
}

Key points

  • A char holds exactly one character, never a whole word.
  • Use char for a single letter, digit, or symbol.
  • Adding a number to a char gives an int result.
  • Console.Write prints a value and stays on the same line.

In this example, mathGrade holds the char A and artGrade holds the char B. The first line joins the label Math: with A and prints Math: A. The second line joins Art: with B and prints Art: B.

Note

Text in double quotes is a string, not a char. A string can hold many characters.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz C# Compiler

Exercise

What is the result of 'A' + 1?