Characters in C#

Notes:

Characters in C# Programming Language :

Character:
- A symbol or sequence of symbols enclosed in between pair of single quotations is considered as a character in C#

C# allows us to work with both ASCII character set as well as Unicode character set.

ASCII character set:
- ASCII stands for American Standards Code for Information Interchange
- ASCII is a 7 bits character set
- Using ASCII character set we can represent maximum 128 characters
- Using ASCII character set we can represent characters available only in the English language
- ASCII character set has a unique number associated to each character in it

Unicode character set:
- Unicode stands for Universal code
- Unicode is a variable length character set (i.e. 8bits, 16bits or 32bits)
- Using Unicode character set we can represent almost all characters from almost all written languages present in the world
- Unicode character set has a unique code associated to each character in it

- To store and process characters; we take help of char data type
- char data type in C# is of 2 bytes (i.e. 16 bits); which is sufficient to store and process more than 65K+ characters

Syntax for declaring and initializing a character type variable:
char nameOfVariable = ’value’;
where
- value can be any symbol from the ASCII character set
- value can be any Unicode value from the Unicode character set

Ex:
char ch = ’A’;
OR
char ch = ‘\u0041’;

Example Code 1:

using System;

namespace CharactersDemo
{
class Program
{
static void Main(string[] args)
{
// char ch = 'a';
// Console.WriteLine("ch = " + ch); // ch = a

// char ch = (char) 97;
// Console.WriteLine("ch = " + ch); // ch = a

//System.Console.OutputEncoding = System.Text.Encoding.Unicode;

// char ch = '\u0041';
// Console.WriteLine("ch = " + ch); // ch = A

// char ch = '\u0042';
// Console.WriteLine("ch = " + ch); // ch = B

// char ch = '\u0021';
// Console.WriteLine("ch = " + ch); // ch = !

// char ch = '\u00A9';
// Console.WriteLine("ch = " + ch); // ch = copyright symbol

// char ch = '\u263A';
// Console.WriteLine("ch = " + ch); // ch = smiley symbol

// char ch = '\u2665';
// Console.WriteLine("ch = " + ch); // ch = heart symbol

System.Console.OutputEncoding = System.Text.Encoding.ASCII;

char ch = 'Z';
Console.WriteLine("ch = " + ch); // ch = Z

Console.ReadKey();
}
}
}