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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

chr: S
Size of a char variable: 2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}
}

chevron_right


Output:

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

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Error:

Too many characters in character literal




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.