C# Char: Everything You Need to Know about Characters in C#

c sharp charC# is a simple programming language that is aimed at those who wish to develop applications based on Microsoft’s .NET platform. What makes C# simple and easy to learn is the fact that the language  is a direct descendant of Java, while also carrying a lot of C and C++ family traits. As such, anyone with exposure to these programming languages will be able to connect instantly with C# courses. C# was developed by Microsoft as part of its .Net initiative and has since gained ECMA and ISO certifications. It is a general purpose object oriented programing language that conforms to Common Language Infrastructure – a key aspect of .Net technology that allows an application to be written in any of the several commonly used programming languages for use on any operating system while requiring a common run-time program rather than a specific one for their execution.

In today’s tutorial, we are going to discuss the Char type in C#. We assume at least beginners level understanding of C#. If you’re completely new to it, do go over and first check out this introductory course to C#.

What is a CHAR Type?

Char represents a character value type and holds a single Unicode character value. It is 2 bytes in size. This is a built-in value type in C#. What this means is that the Char type is integral to the C# programming language and is not one that has been defined by the user. Also, Char is a value type since it actually stores the value in the memory that has been allocated on the stack. This is unlike reference type where the stack actually contains the reference or address of the variable while the object itself resides in the heap. To learn more about character data type and how it’s used, you can check out this course on C#.

Example 1 : How to Assign and Print a Char Variable

Here is a simple program to show you how char type can be used:

using System;
class Program
{
static void Main()
{
//Store character ‘a’ in variable temp_var
char temp_var = ‘a’;
//Store unicode 64 ie A, converted to a char type, in variable temp_var
char temp_var = (char)64;
//Print it out on the screen
Console.WriteLine(A);
}
}

The program is perhaps the simplest that it can be, but it does point out the major elements you need to understand.

The first two statements inside Main()   show how Char variables are defined and assigned values. Here temp_var is the name of the variable and it’s defined to be of type char. What this means is that temp_var will only be able to hold a single character value, which is assigned within single quotes. While the first statement assigns the value ‘a’ directly to the variable temp_var, the second one assigns the Unicode character corresponding to the uppercase letter A.

Example 2 : How to Assign and Print a Char Variable

Let’s take a look at another program to further clarify the char data type:

using System;
class Program
{
static void Main()
{
// Assigning an actual character with the single quotes
char value = ‘a’;
// Print the actual value of the char variable
Console.WriteLine(value);
// Print the unicode integer value corresponding to 'a
Console.WriteLine((int)value);
// Print the 
Console.WriteLine(value.GetType());
}
}
Output:
a
97
System.Char

This is similar to the first program we wrote. Like before, we assigned the character ‘a’ to the variable ‘value’. After that, we have printed it out in different ways. Note that the second WriteLine prints ’97’ – which is the Unicode value of ‘a’. This just goes to show how char variables are stored internally. The third WriteLine shows you can actually check what type your variable really is. The GetType is a method that points out the most derived type of the object, which in this case is Systems.Char.

Example 3: Arrays With Char Types

Though we showed you two simple examples above, it is extremely rare to use the char data type in C# the way it has been shown. Instead, it is used more common to use character arrays to store a string data. An array is just a cohesive set of data elements. So a character array, is a set of characters, stored together, in a single variable. Individual members of the character array are accessed via an index. Let’s understand this better with an example.

using System;
class Program
{

static void Main()
{
// Different ways to assign values to a character array 
// Method 1
char[] array1 = { 'a', 'b', 'c' };

// Method 2
char[] array2 = new char[] { 'l', 'm', 'n' };

// Method 3
char[] array3 = new char[4];
array3[0] = 'x';
array3[1] = 'y';
array3[2] = 'z';
array3[3] = 'a';
// Let's print these arrays
Console.WriteLine(array1);
Console.WriteLine(array2);
Console.WriteLine(array3);
}

}
Output:
a b c
l m n
x y z a

In this example, we show you three different ways of assigning values to character arrays. The first one, array1 is a direct assignment. You can just list the characters (enclosed in single quotes) within curly braces, using a comma to separate each of them. This is the most common approach when you have short pre-defined string that you want to store in the character array.

In the second method, we take a more cautious approach, and we use “new char[]” to specify that the characters following it are part of a character array. This is a good programming practice, though not mandatory.

The third method is most commonly used in well structured, larger programs. We first define array3 to be a new character array, with space for 4 characters. Then, at a later point in the program we actually assign the values. This is usually separated by a few other lines of code in between. We’ve just made it simpler to show you.

While working with arrays of type char, note that the array index always starts from 0. To learn more about arrays and how to use them, you can take this intermediate course on C#.

Example 4: Converting String Data Type to Char Array Type

C# also has a string data type. Strings are pretty similar conceptually to character arrays. In some situations you may need to convert from one to the other. This course can show you how to handle strings in C#. In the meanwhile, here is a program that helps you convert string data type to char array type:

using System;
class Program
{
static void Main()
{
// Assigns string value to the string variable a.
string a = "C SHARP";

// Convert string to array usingToCharArrayfunction.
char[] array = value.ToCharArray();

// UsingLoop to traverse through array.
for (int i = 0; i<array.Length; i++)
{
 // Get character from array.
 char letter = array[i];
 // Print each letter on screen
 Console.Write("Letter: ");
 Console.WriteLine(letter);
}
}
}
Output:
C
S
H
A
R
P

 

We hope these examples have given you a good idea about how to use character variables in C#. In addition, there are several methods that can be used on the char data type within the .Net framework (to learn more about C# and the .Net framework, you can take this course). These include but not limited to:

char.IsControl
char.IsDigit
char.IsLower
char.IsSeparator
char.IsWhiteSpace
char.ToLower
ToChar
We hope this tutorial has given you a good framework to understand the Char data type in C# and how to use it. Do try out these examples on your own to get a better mastery over C#.