C# language uses char variable type to store values of type character. The C# compiler allocates 16 bits (2 bytes) to store a char variable, which is similar to the ushort type. The char variable type can store integer value types ranging from 0 to 65535. The following image shows the representation of such a variable.
You can assign values to char variables like this:
1 |
char letter = 'A'; |
The second way of assigning a value to a char variable is casting an integer type into a char type (converting one variable type into another variable type).
1 |
char c = Convert.ToChar(65); //ASCII value 65 equals to character A |
Char variable type can only store one letter at a time. To store more characters, you have to declare a string, which we will explain later.
The concepts explained in this lesson are also shown visually as part of the following video:
EXERCISES
1. Declare a variable of type char and assign it as a value the character which has Unicode code 72 (use the Windows calculator in order to find hexadecimal representation of 72).
Tags: variable types, variables