Lesson 2.14

Char Data Type C# - Programming Example


Objective

What is Char data type in c#?
How to declare and initialize
Programming examples
Conversion and Casting

What is Char Data Types in C#?

"Char is 16 bit numerical value which is a instance of System.Char class. It is used to represent Unicode character or written language throughout the word. It can store only single character."

TypeRangeSize.Net Framework type
charU+0000 to U+FFFFUnicode 16-bit characterSystem.Char

Declaration and Initialization

Important Notes: A char literal can store following type of values.

1. Character literals
2. Hexadecimal escape sequence
3. Unicode value
4. Integral value

You can declare char variable as follows:

		  
Char c1=’k’; //Keep value under single quotes (')
		  

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace char_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            char c1 = 'K';
            Console.WriteLine("Character Literal: "+ c1);

            char c2 = '\x0045';
            Console.WriteLine("Hexadecimal Value: " + c2);

            char c3 = '\u0048';
            Console.WriteLine("Unicode Value: "+ c3);

            char c4 = (char)67;
            Console.WriteLine("Cast From Integral Value: "+ c4);
            
            
            Console.ReadLine();
        }
    }
}

		  

Output

Character Literal: K
Hexadecimal Value: E
Unicode Value: H
Cast From Integral Value: C
_

Conversion

1. Implicit Conversion - No need to convert when left hand side data type 
  is:   char
  
  Example:
  
  char a=5, b=6;
  char result=a+b;

2. Explicit Conversion - You need to convert when left hand side data type is: byte, sbyte, short, ushort, int, uint, long, char and char Example: char a=5, b=6; int result=(int)(a+b); Note: When converted char value into other integral type, the point precision gone.

Passing char value as parameter

When calling a function which requires char value in parameter you must cast value into respective datatype to avoid compilation error..

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace char_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            char var1=342.52M;
            ShowNumber((int)(var1)); // Correct(√)

            ShowNumber(var1); // Incorrect(×)
        }

        public static void ShowNumber(int num1)
        {
            Console.WriteLine(num1);
            Console.ReadLine();
        }
    }
}
		  

Output

342
_

It is best habbit to cast or convert data into respective datatype everywhere. It will save you from unnecessary runtime errors and makes your code more robust.

Explanation

In the above example we have created a function called ShowNumber(int num1) which requires an integer type value as parameter and then display the number.

We have called this function in our main class in which we casted char value into int type before passing.
 char var1=342M;
 ShowNumber((int)(var1)); // Correct(√)

 If we don’t cast it will raise compilation error as follow :
 ShowNumber(var1); // Incorrect(×)

 Compilatation Error :Error 1: The best overloaded method match for 'char_data_type.Program.ShowNumber(int)' has some invalid arguments
 Error 2: cannot convert from 'char' to 'int'
  

Summary

char is 128-bit data type which stores value between (-7.9 x 1028 to 7.9 x 1028) / (100 to 28). It is mostly used calculating financial or monetary data. In the next chapter we will discuss on string data type.

BackNext