Implicit And Explicit Type Casting In C#

Introduction

 
Imagine you have 3 containers of different sizes. Say a small-container, medium-container & large-container. 
 
Let's try to fit containers one into another, the order that we must follow is: small > medium > large. Which means smaller container can be fit inside medium & medium container can be easily fit inside a larger container.
 
This same concept is called type casting in programming. 
 
In C# we have different kinds of data-types - (Containers), which have different sizes. The following table represents the different sizes of data-type in C#.
 
 DataType  Size in Byte(s)
 byte  1
 sbyte (signed byte)  1
 short  2
 ushort (unsigned)  2
 int  4
 uint (unsigned)  4
 float  4
 double  8
 long  8
 ulong (unsigned)  8
 bool  2
 char  2
 string (object)  4
 object type  4
 DateTime  8
 decimal  24
 
As per the table, let's try to fit small data-types into bigger ones.
 
Printing different kinds of data-types in C#.
  1. using System;  
  2.   
  3. namespace LeranCSharp  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             byte thisIsByte = 1;  
  10.             Console.WriteLine("byte: "+ thisIsByte);  
  11.             sbyte thisIsSignedByte = -1;  
  12.             Console.WriteLine("sbyte: " + thisIsSignedByte);  
  13.             short thisIsShort = -2;  
  14.             Console.WriteLine("short: " + thisIsShort);  
  15.             ushort thisIsUnsignedShort = 2;  
  16.             Console.WriteLine("ushort: " + thisIsUnsignedShort);  
  17.             int thisIsInt = -4;  
  18.             Console.WriteLine("int: " + thisIsInt);  
  19.             uint thisIsUnsignedInt = 4;  
  20.             Console.WriteLine("uint: " + thisIsUnsignedInt);  
  21.             float thisIsFloat = 8;  
  22.             Console.WriteLine("float: " + thisIsFloat);  
  23.             double thisIsDouble = 10.5;  
  24.             Console.WriteLine("double: " + thisIsDouble);  
  25.             long thisIsLong = -123456;  
  26.             Console.WriteLine("long: " + thisIsLong);  
  27.             ulong thisIsUnsignedlong = 123456;  
  28.             Console.WriteLine("ulong: " + thisIsUnsignedlong);  
  29.             bool thisIsBool = true;  
  30.             Console.WriteLine("bool: " + thisIsBool);  
  31.             char thisIsChar = 'c';  
  32.             Console.WriteLine("char: " + thisIsChar);  
  33.             string thisIsString = "string";  
  34.             Console.WriteLine("string: " + thisIsString);  
  35.             object thisIsObject = new object();  
  36.             Console.WriteLine("object: " + thisIsObject);  
  37.             DateTime thisIsDateTime = new DateTime();  
  38.             Console.WriteLine("DateTime: " + thisIsDateTime);  
  39.             decimal thisIsDecimal = 1234567890;  
  40.             Console.WriteLine("decimal: " + thisIsDecimal);  
  41.         }  
  42.     }  

The output of the above program:
 
 
So now that we are clear on different kinds of data types, let's understand what typecasting means.
 
It allows assigning the value of one data-type to other data-type, either implicitly or explicitly.
  • Implicit type casting: assigning the value of the smaller type to the larger type. i.e. fitting smaller containers into bigger ones. but both data types have to be compatible with one another. (you can not convert bool into string datatype, because they both are not compatible with each other.)
  • This table will explain to you that what are possible types of implicit conversions for a few datatypes.
 byte  short, int, long, float, double : all numeric types
 short  int, long, float, double : can not convert into byte as it is a smaller container.
 int  long, float, double : same reason as above.
 long  float, double : same reason as above.
 float  double : same reason as above.
 char  int, long, float, double
 
So we get sequence like this: byte > short > char > int > long > float > double
 
Let's take one example of float & double.
 
First, an implicit conversion.
 
Just use equal to operator, and that's that.
  1. Console.WriteLine("---------- Conversions -----------");  
  2. Console.WriteLine("---------- Impilcit double -----------");  
  3. thisIsDouble = thisIsFloat;  
  4. Console.WriteLine("double: " + thisIsDouble); 
Let's see if double is assigned with float value.
 
Yes! it does.
 
Now when we try to convert the larger container into smaller ones, the compiler won't allow us.
 
 
The compiler not only gives us an error but it also suggests some solutions. here it is asking us to perform explicit type conversion. let's do that then.
  1. thisIsFloat = (float)thisIsDouble;  
  2. Console.WriteLine("float: " + thisIsFloat); 
Here, in order to explicitly convert, just follow the above syntax. i.e. put the type into parentheses and see if this works.
 
 
Who are we kidding, of course, it works.
 
There is one more way to perform the explicit conversion. we can parse types.
  1. thisIsFloat = float.Parse(thisIsDouble.ToString());  
  2. Console.WriteLine("float: " + thisIsFloat); 
If I tell you there is one more way to perform the explicit conversion. would you believe it?
 
You can use conversion methods. Let's try to convert char to int, but this time with convert methods.
  1. char thisIsChar = 'A';  
  2. Console.WriteLine("char: " + thisIsChar);  
  3.   
  4. thisIsInt = Convert.ToInt32(thisIsChar);  
  5. Console.WriteLine("int: " + thisIsInt); 
This conversion will print ASCII code for a specific character. In this case, we have capital A, so it should print 65.
 
 
 Alright, so far so good.
 
 Let's see what different kinds of conversion methods C# provides.
  1. using System;  
  2.   
  3. namespace LeranCSharp  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Convert.ToBoolean(1);  
  10.             //Converts a type to a Boolean value, where possible.  
  11.             //converts any number into True, except string or char. returns false for 0       
  12.   
  13.             Convert.ToByte(2);  
  14.             //Converts a type to a byte.  
  15.   
  16.             Convert.ToChar(5);  
  17.             //Converts a type to a single Unicode character.  
  18.   
  19.             Convert.ToDateTime(new DateTime(2020, 5, 2));  
  20.             //Converts a type (integer or string type) to date-time structures.  
  21.   
  22.             Convert.ToDecimal(10);  
  23.             //Converts a floating-point or integer type to a decimal type.  
  24.   
  25.             Convert.ToDouble(15);  
  26.             //Converts a type to a double type.  
  27.   
  28.             Convert.ToInt16(5);  
  29.             //Converts a type to a 16-bit integer.  
  30.   
  31.             Convert.ToInt32(15);  
  32.             //Converts a type to a 32-bit integer.  
  33.   
  34.             Convert.ToInt64(25);  
  35.             //Converts a type to a 64-bit integer.  
  36.   
  37.             Convert.ToSByte(5);  
  38.             //Converts a type to a signed byte type.  
  39.   
  40.             Convert.ToSingle(22);  
  41.             //Converts a type to a small floating point number.  
  42.   
  43.             Convert.ToString(22);  
  44.             //Converts a type to a string.  
  45.   
  46.             Convert.ToUInt16(20);  
  47.             //Converts a type to an unsigned int type.  
  48.   
  49.             Convert.ToUInt32(18);  
  50.             //Converts a type to an unsigned long type.  
  51.   
  52.             Convert.ToUInt64(16);  
  53.             //Converts a type to an unsigned big integer.  
  54.   
  55.         }  
  56.     }  

Conclusion

 
Now we know what different types of datatypes used in C#, and how we can easily convert one into another, as well as the different techniques for an explicit conversion.
 
I hope this article has been helpful to you for understanding typecasting. Please apply them whenever necessary.
 
Thank you so much. Keep coding.


Recommended Free Ebook
Similar Articles