Data Types: Type conversion

Data Types: Type conversion
C# is a strongly typed language which means you have to give a data type when you declare a variable. But there might occur a situation where you want to share data between different type of variables for this purpose you use type casting.

Type Casting

Converting one type of variable to another is called type casting. You can do this by casting one type of variable to another type of variable.

Types of Type casting

There are two types of type casting.
  1. Implicit type casting
  2. Explicit type casting
Implicit Type Casting

These are conversion about which you don’t have to worry. C# compiler deals them for you in type-safe manner. These are done when you care casting a smaller data type or larger data type.

Example

static void Main(string[] args)
   {
         byte b = 100;
         short s = 200;
         int i = 300;

         i = s;
         s = b;
         Console.WriteLine("The value of i is {0} and value of s is {1}.",i , s);
         Console.ReadLine();
    }
In above program you can see that you can assign value of short to int and value of byte to short because byte is 8 bits and short is 16 bits. So the 8 bit data can be stored in 16 bit space. Same goes for int which is 32 bits and can store short value which is 16 bit.

Output

Type conversion

Explicit Type Casting

These are conversion which can’t be done automatically. For these conversions you have to cast the data manually to tell the compiler that you are ok with this conversion because in these types of conversion some data might be lost. Converting larger data types to smaller data types are examples of explicit type casting.

Casting operator

When you want to cast a variable to your desired data type you have to put your desired data type in parenthesis and put it in front of the variable you are casting. E.g. byte b = (byte) i;

Example

static void Main(string[] args)
        {
            byte b = 100;
            short s = 200;
            int i = 300;

            b = (byte)s;
            Console.WriteLine("The value of b is {0}.",b);

            b = (byte)i;
            Console.WriteLine("The value of b is {0}.", b);

            Console.ReadLine();
        }
Output

Type conversion

  1. Here you can see if you want to assign the value of short to byte you have to explicitly cast short to byte. For which you use casting operator.
  2. You have to use it because short is 16 bits and byte is 8 bits and you can’t put larger thing in smaller basket.
  3. To solve this problem we use casting operator and tell the compiler that we only want first 8 bits and you can leave all other part of that variable.
  4. In above example you can see that when the variable “s” is type casted into byte then the value of variable “b” is still 200 because that is stored in first 8 bits which is now allocated to variable “b”
  5. But when variable “i” is type casted into variable “b” then the value is reduced to 44. It happened because the first 8 bits of integer only holds 44 that is allocated to variable “b” and all other bits are just ignored.
<< Data Types: Character and String
Tags: C-sharp
comments powered by Disqus