Open In App

char keyword in C#

Keywords are the words in a language that are used for some internal process or represent some predefined actions. char is a keyword that is used to declare a variable which store a character value from the range of +U0000 to U+FFFF. It is an alias of System.Char.

Syntax:



char variable_name = value;

char keyword occupies 2 bytes (16 bits) in the memory.

Example:



Input: S

Output: chr: S
        Size of a char variable: 2 

Input: G

Output: Type of chr: System.Char
        chr: G
        Size of a char variable: 2

Example 1:




// C# program for char keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'S';
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print the size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
    }
}

Output:

chr: S
Size of a char variable: 2

Example 2:




// C# program for char keyword
using System;
using System.Text;
  
namespace geeks {
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'G';
  
        // to print the type of variable
        Console.WriteLine("Type of chr: " + chr.GetType());
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

Output:

Type of chr: System.Char
chr: G
Size of a char variable: 2

Example 3:




// C# program for char keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        char chr = 'Geeks';
  
        // to print value
        Console.WriteLine("chr: " + chr);
  
        // to print size of a char
        Console.WriteLine("Size of a char variable: " + sizeof(char));
    }
}

Error:

Too many characters in character literal


Article Tags :
C#