C# Variables - Characters

Updated: May 7, 2020

In C#, the Char data type represents a single Unicode character. At a high level, Unicode is a universal standard for defining the way characters are encoded by the computer. Familiar characters of every language can be represented by the char data type.

Declaration and Initialization

The char keyword is used to declare a variable that will store a character value. The most common way to initialize a char variable is by using a character literal, meaning you provide the literal character value for the variable.

The critical thing to remember is that working with C#, character literals must be enclosed within single quotes ‘ ‘, unlike the double quotes “ “ used for string literals.

 char middleInitial = 'S';
 char luckyNumber = '7';
 char sharpSymbol = '#';

Sometimes, you will want to use a character that can’t be typed in a literal, such as a horizontal tab. This is common when you are working with the way information is displayed. To do this, you need to know the ASCII value for the character then cast that into a character data type. You can learn more about casting and converting from one data type to another in the “Data Type Conversion” blog post.

An internet search for ASCII Table will provide you with many resources to get the ASCII value for any character. The value for the TAB character is 9. With this information, you can add a horizontal tab into your strings to help you align the display.

 char tabSpace = (char)9;

By using the TAB character in this code, I can ensure the first, middle, and last names align properly in my display.

Escape Sequences

Some characters are used by C# to write the code and can not be simply assigned to a variable. An excellent example of this would be the single quote ‘ character. The following line of code will not compile.

 char singleQuote = ''';

To use the single quote as a literal value, you need to “escape” it. Escaping a character tells Visual Studio to ignore the usual meaning of character and use it as a literal. In this example, we want Visual Studio to ignore the standard meaning of the middle quote.

To escape a character, you place a backslash \ before it. When Visual Studio sees the backslash, it will treat the next character as a literal. For the code to work, it would be changed to this:

char singleQuote = '\'';

Here is a list of characters that need to be escaped if you want to ignore the usual meaning of the character and use the alternate meaning.

Char Methods

The strength of the Char object comes with its methods.

Character Check

Char provides several methods to check for a particular character type. Each of the following methods will return a Boolean TRUE/FALSE value as a result of the check.

  • IsLetter – returns TRUE if the character is a valid letter

  • IsLetterOrDigit – returns TRUE if the character is alphanumeric

  • IsLower/IsUpper – returns TRUE if the case of the character matches the method call

  • IsPunctuation – returns TRUE if the character is categorized as a punctuation mark

  • IsSymbol – returns TRUE if the character is categorized as a symbol character

  • IsWhitespace – returns TRUE if the character is considered whitespace. Whitespace characters are characters that create spaces for you to the “white” of the page. Whitespace characters include spaces, tabs, and newlines.

  • IsControl – returns TRUE if the character is a control character. Control characters are non-printing characters that control the format of the display. Some examples are carriage return, line feed, section break, column break.

Character Information

Other methods allow you to get information regarding the character.

  • GetNumericValue – returns the numerical ASCII value for the character you specify

  • GetUnicodeCategory – provides the Unicode category in which the character belongs.

 char.GetUnicodeCategory(' ');  

The first line of code will return “OtherPunctuation”. The second returns “SpaceSeperator”.

19 views0 comments

Recent Posts

See All