char type

In this chapter you will learn:

  1. Get to know C# char type
  2. Assign value to a char type

C# char type

char Structure represents a Unicode character. char aliases the System.Char struct. You can call the methods from char or from System.Char.

The following code stores character a to char type variable:

using System;/*from   j  a va  2s  .  c o  m*/

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

The output:

The Char structure has methods to compare char objects, convert the char object to an another type, and determine the Unicode category of a Char object.

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

using System;/*from   jav  a2 s.com*/

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:

Value of a char type

C# uses a 16-bit character type called Unicode.

Unicode defines a character set that is large enough to represent all of the characters found in all human languages. There are no automatic type conversions from integer to char.

Char supplies methods that allow you to process and categorize characters. Char defines the following fields:

public const char MaxValue
public const char MinValue

These represent the largest and smallest values that a char variable can hold. Char implements the following interfaces: IComparable and IConvertible.

using System;/*j  a v a2s . c  o m*/

class MainClass
{
  static void Main(string[] args)
  {
    char MyChar = 'A';
        Console.WriteLine(MyChar);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What value need escape
Home » C# Tutorial » bool, char
bool
bool and if statement
bool value to string
Parse string to bool value
char type
char escape sequence
char case
Compare char value
char min/max value
Is char a digit
Is char a letter
Is char a symbol
Is char a Separator
Is char white space
Convert to char
UTF-16 Character