C# char Type

Use the char type. Chars are values, similar to ushort, that represent characters.
Char. 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. This program performs some operations on a char variable after initializing it to the lowercase letter "a". The lowercase "a" is equal to the integer value 97 in ASCII.

Cast: The program casts the char value to an int value and displays it. This is useful for indexing arrays with characters.

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

Typeof, nameofGetType

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

Struct: Char is a struct in the base class library that is aliased to the char keyword. The "char" keyword aliases the "System.Char" type.

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
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
Static methods. The char type contains many static methods that test or convert chars. Some methods, which start with "Is," return a bool that reveals some aspect of the char.char.IsControlchar.IsDigitchar.IsLetterOrDigitchar.IsLowerchar.IsPunctuationchar.IsSeparatorchar.IsWhiteSpacechar.ToLower
Arrays. 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
Research. 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).

A summary. 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls