BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Character Data Type

The fifteenth part of the C# Fundamentals tutorial returns to the topic of data types. This article examines the character data type that permits us to begin crossing the boundary between numeric values and representations of textual information.

ASCII

In 1967, the American Standard Code for Information Interchange (ASCII) was published. ASCII defines a number to represent each English letter, digit and various punctuation symbols. The code includes a series of non-printable control characters to allow control of devices such as printers. Examples of control characters included character 13, which represents a carriage return and 10 for a line feed.

ASCII uses the range of numbers from zero to one hundred and twenty six to represent both its printing and non-printing characters. Each character can be stored in seven binary digits of information. In most programming languages that support a character data type, a single byte can be used to represent an ASCII character.

Unicode

Unicode is a more recent standard for encoding text. Unicode allows the representation of many more characters than ASCII, including international letters and symbols not used in the English language. With some languages requiring many thousands of characters, a single byte per character is not sufficient for encoding. Unicode therefore uses two or more bytes for larger character sets.

The Character Data Type

The character or char data type defined within C# is used to hold a single Unicode character. Character variables hold a sixteen-bit number representing a letter, digit, symbol or control character. They can also be considered as a numeric data type with similar properties to an unsigned short integer value.

Assignment

Values can be assigned to a character variable using the normal assignment operator (=). As the data type provides a crossover between numeric and textual information, information can be assigned in two ways. To assign the character directly, a single letter, number or symbol can be used, surrounded by apostrophes ('). An integer value may also be used but must be cast as a char.

char letterA;

letterA = 'A';                      // Assign a character directly
letterA = (char)65;                 // Assign a number cast to a char

The example above demonstrates the two methods by which a value can be assigned to a character variable. In fact, the two assignment operations perform the same task; the Unicode character represented by the number sixty-five is the capital letter "A". This can be demonstrated further by converting the resultant character back to a numeric or character representation and displaying it using the Console.WriteLine method.

char letterA;

letterA = 'A';                      // Assign a character directly
Console.WriteLine(letterA);         // Outputs "A";
Console.WriteLine((int)letterA);    // Outputs "65";

C# 2.0 Nullable Character

Earlier in the tutorial we examined the nullable numeric data types that were introduced with the .NET Framework 2.0. The character data type has a nullable equivalent with similar functionality.

char? c;

c = 'A';                            // Assign a non-null character
c = null;                           // Assign an undefined, or null, value;
10 October 2006