C# Char Type

Use the char type. Chars are values, similar to ushort, that represent characters.


The char type represents a single character. It is a value type. Char is similar to an integer or ushort. It is 2 bytes in width.Int, uintshort, ushort

Casting, strings.

A char must be cast when converting to an integer. With an indexer, we access chars in strings. Chars can be incremented in a loop.Strings

A start.

We access a char from "cat." The char at index 0 is the lowercase letter "c." Strings are indexed starting at zero—this is important. We store the char in a local variable.

Also: We test the value of the char variable in an if-statement. Please notice how a char uses single-quotes around it.

Character Literal
C# program that uses char literal using System; class Program { static void Main() { // Get char at first index. char value = "cat"[0]; // If it equals "c" then print something. if (value == 'c') { Console.WriteLine(value + "#"); } } } Output c#

Example 2.

Next, this program performs some operations on a char variable after initializing it to the lowercase letter "a". We see an example of casting the char variable.

Also: We compare the char to another variable. We then obtain its managed Type pointer.

Thus: We prove that when allocated on the managed heap, a char will occupy 2 bytes of storage, equivalent to a ushort integer.

C# program that demonstrates char type using System; class Program { static void Main() { // // Declare a character and test in certain ways. // char value = 'a'; Console.WriteLine(value); Console.WriteLine((int)value); Console.WriteLine(value == 'y'); Console.WriteLine(value.GetType()); Console.WriteLine(typeof(char)); Console.WriteLine((int)char.MinValue); Console.WriteLine((int)char.MaxValue); // // Determine the memory usage for a single char. // long bytes1 = GC.GetTotalMemory(false); char[] array = new char[1000 * 1000]; array[0] = 'a'; long bytes2 = GC.GetTotalMemory(false); Console.WriteLine(bytes1); Console.WriteLine(bytes2); Console.WriteLine(((bytes2 - bytes1) / (1000 * 1000)).ToString() + " bytes per char"); } } Output a 97 (Integer value of char) False System.Char System.Char 0 (MinValue as an integer) 65535 (MaxValue as an integer) 29252 (Memory measurement 1) 2029284 (Memory measurement 2) 2 bytes per char

Example, notes.

The program casts the char value to an integer value and displays it in a decimal form. This is useful for indexing arrays with characters.

Also: It helps with compilation problems when adding characters together or comparing characters.

Tip: The C# language has specific rules relating to char conversions to integers.

Cast, Int


The typeof operator and the GetType virtual method on the char variable in the program both return the value System.Char.

Struct: This type is a struct in the base class library that is aliased to the char keyword.


Note: When you use the char keyword, you can always use System.Char instead as they are the same.

Typeof, nameofGetType

Char memory usage.

The final part of the program does a test. It allocates one million chars in an array on the managed heap, measuring memory before and after.

Then: It divides the memory difference by one million. The array is touched in memory with an assignment to ensure it is not optimized out.

And: The test indicates that a char requires two bytes in memory when part of an array.


Loop, char range.

Look at this program. It is pretty exciting. It loops over the lowercase ASCII range from "a" to "z." These chars have integer forms, so can be incremented.
C# program that loops over character rage using System; class Program { static void Main() { // Loop over all lowercase letters. for (char c = 'a'; c <= 'z'; c++) { Console.Write(c); Console.Write(' '); } } } Output a b c d e f g h i j k l m n o p q r s t u v w x y z


The char data type is used as part of an array or string. You can easily convert character arrays to strings, and strings to char[] arrays.

Tip: You will often use character arrays in C# programs. These can be useful for optimizations, or for creating buffers.

Char ArrayToCharArrayConvert Char Array, String


Char is a special type. I researched it in the C# specification. I found a char is similar to a ushort but with restrictions.

Usage: At a high level, the types are used differently. This is enforced by the compiler.

Quote: Although char has the same representation as ushort, not all operations permitted on one are permitted on the other (The C# Programming Language).

Character is important.

So is the char type. For compatibility, a char in the C# language is 2 bytes. This makes it less efficient. But speed is less important than a program that works.
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen,