1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Tutorial - Learn the Fundamentals of Programming

By David Bolton, About.com

8 of 10

A Brief Look at Char Types

The Char Type

In the old days, basically until the mid 1990s, ASCII was the most popular encoding for characters. By encoding I mean how a numeric value is treated as a character. To a CPU, all data and instructions are just numbers in memory. The value 6810 in memory is just held as a series of bits, with the bit pattern 010001002. But if it is displayed as a character it has the value 'D'. The CPU doesn't know or care whether we want to treat this value as a bit pattern, part of a larger number, a single character or a text string.

Before Unicode came along, one byte generally held a single ASCII character. However foreign languages have many odd characters and no way could these be mapped to 256 different values. So Unicode was invented, to use 16 bits for a character. In fact 32 bits is possible but I've not encountered it yet.

32 Bit Operating Systems like Windows NT and Linux used 16 bit character representations for text in addition to the 8 bits and C++ for instance has char (8 bit) and wchar_t (16 bits). C# doesn't use 8 bit characters, instead all chars are a single Unicode character which is 16 bits.

There are several ways to declare a char. These 4 lines below each set variables char1 to char4 to the letter 'W' in 4 different ways then print them out. The output is W W W W.

char char1 = 'W'; // Character literal
char char2 = '\x0057'; // Hexadecimal
char char3 = (char)87; // Cast from integral type
char char4 = '\u0057'; // Unicode

Console.WriteLine("{0} {1} {2} {3}",char1,char2,char3,char4) ;
Chars have their uses, particularly if you are doing text processing. A future lesson will look at strings and I'll cover chars then in more depth. Generally you will not use them that much.

That concludes this lesson- the next one will be about Value and Reference Types.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. A Brief Look at Char Types

©2009 About.com, a part of The New York Times Company.

All rights reserved.