HomeSearch

C# char.IsControl (If Char Is Special)

Test the char.IsControl method. Find all control characters using a for-loop.

Char.IsControl.

The char.IsControl method detects control characters. Certain characters do not represent a letter. Instead a char can represent a vertical space or even a beep. IsControl detects these codes.Char

Note: The char.IsControl method returns true or false depending on if the character is a non-printing character.

Bool

The program

uses the char.IsControl method. It contains a loop through all the possible values of a char data type. Those characters are tested to see if they are control characters with IsControl.For

Tip: Control characters are special characters. Letters like A and B are not control characters.

Results: We see the control characters in the character set in the char type are equal to the decimal representations of 0-31, and 127-159.

Note: The characters \r and \n are equal to 13 and 10, and these are shown to be control characters.

Finally: We see the use of the char.IsControl method that receives 2 arguments, a string and index argument.

Overload
C# program that tests IsControl method using System; class Program { static void Main() { // Loop through all possible char values and test for control. for (int i = 0; i < char.MaxValue; i++) { char c = (char)i; if (char.IsControl(c)) { Console.WriteLine("Is control: {0}", i); } } // See if carriage return is a control character. Console.WriteLine("Is control: {0}", char.IsControl("\r\n", 0)); } } Output Is control: 0 Is control: 1 Is control: 2 Is control: 3 Is control: 4 Is control: 5 Is control: 6 Is control: 7 Is control: 8 Is control: 9 Is control: 10 Is control: 11 Is control: 12 Is control: 13 Is control: 14 Is control: 15 Is control: 16 Is control: 17 Is control: 18 Is control: 19 Is control: 20 Is control: 21 Is control: 22 Is control: 23 Is control: 24 Is control: 25 Is control: 26 Is control: 27 Is control: 28 Is control: 29 Is control: 30 Is control: 31 Is control: 127 Is control: 128 Is control: 129 Is control: 130 Is control: 131 Is control: 132 Is control: 133 Is control: 134 Is control: 135 Is control: 136 Is control: 137 Is control: 138 Is control: 139 Is control: 140 Is control: 141 Is control: 142 Is control: 143 Is control: 144 Is control: 145 Is control: 146 Is control: 147 Is control: 148 Is control: 149 Is control: 150 Is control: 151 Is control: 152 Is control: 153 Is control: 154 Is control: 155 Is control: 156 Is control: 157 Is control: 158 Is control: 159 Is control: True

Internals.

Let's peek inside the internals of the char.IsControl method in the .NET Framework using IL Disassembler. The IsControl method behaves differently depending on whether the character is a Unicode character or not.

And: If the character is not a Unicode character, a byte[] lookup table is used.

Then: The array is accessed by the value of the character as an index, and this is translated to true or false.

IL Disassembler

How to examine this method.

Compile this program in Visual Studio. Then open the EXE file in the IL Disassembler program. Then click on the IsControl method name to see the internals.

Summary.

We examined the char.IsControl method in the C# language. This provides a way to test certain characters to see if they are "control" characters that provide behavior separate from just a letter or similar character.

Review: Control characters can indicate a beep or vertical space as well. This is only rarely a useful feature.

Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.