C# char type

In this chapter you will learn:

  1. What is the char type in C#
  2. How to create literal for C# char type
  3. What are the escape sequences
  4. Char Conversions
  5. Example: uses methods from Char type

Description

C#'s char type represents a Unicode character and occupies two bytes. Aliasing the System.Char type.

Literial

A char literal is specified inside single quotes:

char c = 'A'; // Simple character

The following code stores character a to char type variable:


using System;//from w  ww  .  j  ava  2 s.c  om

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

The output:

Escape sequences

Escape sequences express characters that cannot be expressed literally.

An escape sequence is a backslash followed by a character with a special meaning. For example:


char newLine = '\n'; 
char backSlash = '\\'; 

The escape sequence characters are shown in the following table.

CharMeaningValue
\'Single quote0x0027
\"Double quote0x0022
\\Backslash0x005C
\0Null0x0000
\aAlert0x0007
\bBackspace0x0008
\fForm feed0x000C
\nNew line0x000A
\rCarriage return0x000D
\tHorizontal tab0x0009
\vVertical tab0x000B

The \u (or \x) escape sequence lets you specify any Unicode character via its fourdigit hexadecimal code:

The following code shoes how to create unicode char literal:


using System;//  w w  w  .j  a v  a2 s  . co m

class Program
{
    static void Main(string[] args)
    {
        char copyrightSymbol = '\u00A9';
        char aSymbol = '\u03A9';

        Console.WriteLine(copyrightSymbol);
        Console.WriteLine(aSymbol);

    }
}

The code above generates the following result.

Char Conversions

An implicit conversion from a char to a numeric type works for the numeric types that can accommodate an unsigned short.

For other numeric types, an explicit conversion is required.

Example

The following code shows some of the basic methods from Char structure.


using System;/*from  w  w w  .  java 2  s .co  m*/

public class Test{
  public static void Main() {
    char chA = 'A';
    char ch1 = '1';
    string str = "java2s.com"; 

    Console.WriteLine(chA.CompareTo('B'));      
    Console.WriteLine(chA.Equals('A'));        
    Console.WriteLine(Char.GetNumericValue(ch1));  
    Console.WriteLine(Char.IsControl('\t'));    
    Console.WriteLine(Char.IsDigit(ch1));      
    Console.WriteLine(Char.IsLetter(','));      
    Console.WriteLine(Char.IsLower('u'));      
    Console.WriteLine(Char.IsNumber(ch1));
    Console.WriteLine(Char.IsPunctuation('.'));  
    Console.WriteLine(Char.IsSeparator(str, 4));
    Console.WriteLine(Char.IsSymbol('+'));      
    Console.WriteLine(Char.IsWhiteSpace(str, 4));  
    Console.WriteLine(Char.Parse("S"));        
    Console.WriteLine(Char.ToLower('M'));      
    Console.WriteLine('x'.ToString());        
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is the String type
  2. How to create string literal
  3. How to compare to string values
  4. Escape Sequences
  5. Verbatim string literals
  6. String concatenation
  7. Null and empty strings
  8. Accessing characters within a string
Home »
  C# Tutorial »
    C# Language »
      C# Data Types
C# Predefined Type Taxonomy
C# Numeric Types
C# Numeric Literals
C# Numeric suffixes
C# float and double
C# Float and Double Special Values
C# decimal
C# Numeric Conversions
C# Bool Type
C# char type
C# String Type