Value Char

The char type represents a character. In the C# language, char is a value type and similar to an integer. It is two bytes in width and it stores an unsigned value. One common problem with it is the requirement to cast when converting to an integer.
C# KeywordsAlthough char has the same representation as ushort, not all operations permitted on one are permitted on the other. Hejlsberg et al., p. 129

To start, this program introduces the char type in the C# language. First, it access the letter at index 4 of the string literal; this is equal to the character literal 's'. It tests the char variable in an expression in the if-statement.
Character LiteralProgram that uses char literal [C#]
using System;
class Program
{
static void Main()
{
char c = "perls"[4];
if (c == 's')
{
Console.WriteLine(true);
}
}
}
Output
TrueNext, 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, comparing it to another variable, obtaining its managed Type pointer, and then its minimum and maximum value. We prove that when allocated on the managed heap the char type will occupy two bytes of storage, equivalent to a ushort integer.
Program that demonstrates char type [C#]
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
Overview. The program shows how you can cast the char value to an integer value and display it in a decimal form. This is useful for indexing arrays with characters and can also help with compilation problems when you are trying to add characters together or comparing characters. The C# language has specific rules relating to char conversions to integers.
Cast to IntType pointer for char type. The typeof operator and the GetType virtual method on the char variable in the program both return the value System.Char. This type is a struct in the base class library that is aliased to the 'char' keyword. When you use the 'char' keyword, you can always use System.Char instead as they are the same.
Typeof Operator GetType Method
Char memory usage. The final part of the program does a test to determine the byte size of the char data type when stored in an array. It allocates one million chars in an array on the managed heap, measuring memory before and after this allocation. It then divides the memory difference by one million. The array is touched in memory with an assignment to ensure it is not optimized out. The test indicates that a char contains two bytes in memory.
GC.GetTotalMemory Usage Divide NumbersThe char data type in the C# programming language is rarely used alone and you will usually be using it as part of an array or string type. You can easily convert character arrays to strings, and strings to char[] arrays. You will commonly want to use character arrays in your C# programs. These can be useful for optimizations, or even for regular usage when creating buffers.
Char Array Use ToCharArray Method, Convert String to Array Convert Char Array to String
There are many public static methods on the char type in the .NET Framework. Here we reveal several of these methods and how they can be used to your advantage.
char.IsControl Use char.IsDigit Method, Test Number Chars char.IsLetterOrDigit Method char.IsLower Methods char.IsSeparator char.IsWhiteSpace Method char.ToLower Methods ToChar Extension Method Example
There are some subtleties to how chars work with the string type in the C# language. These articles attempt to provide answers to some common questions about chars and the string type.
String Char Tips Switch Char, Conditional Character Test CharEnumerator Tips Convert Char to StringCount characters. It is sometimes useful to gather statistics about the frequency of characters or the total number of non-whitespace characters. The first article draws upon the methodologies used by Microsoft Word to implement character counting; the second develops a frequency table of characters.
Count Characters in String Count Letter FrequenciesRemove characters. How can you programmatically remove a character from a string? You may also need to remove characters based on some other criterion, such as their frequency. These articles examine this question and provide answers for you.
Duplicate Chars, Remove Characters Remove Char ExamplesWhat characters are available in the ASCII character set? This article demonstrates code that programmatically prints each ASCII character and renders it as an HTML table.
ASCII Table
An important optimization to understand is the lookup table optimization: in this technique, you replace a computation with a lookup in a precomputed array. This can improve performance in certain situations.
Char Lookup Table Char Lowercase OptimizationOther performance tips. There are other performance optimizations you can use on the char type. These articles show how you can optimize StartsWith, and also how you can combine multiple chars into a string.
Char Test With String Char Combine Method
We looked at the char data type in the C# language targeting the .NET Framework. When allocated on the managed heap, the char type will occupy two bytes of space. This makes it twice as large as characters in some C-like languages. And the char type can contain as many different values as a short integer or ushort integer.