Home > C# > Type Casting in C#

C# allows the user to convert one data type into another. It is called Type Conversion or Type casting in c# or simply casting in c#.
There are two types of type casting in c#:

  1. Implicit
  2. Explicit

Implicit type casting in C#

C# safely does an implicit type casting. i.e., from smaller data types to larger data types or from derived classes to base classes. i.e., implicit conversions can occur without the programmer’s specifying them; We must take care to prevent unpleasant surprises like some errors or exceptions.
Example Program: –

// Implicit Type casting in C# example 
using System;
namespace typeCasting
{
     class Digit
     {
          byte value;
          public Digit(byte value)
          {
               if (value > 9)
               {
                    Console.WriteLine("Wrong Value Given!!!!!!");
               }
               else
               {
                    this.value = value;
               }
          }
          public static implicit operator byte(Digit d)
          {
               Console.WriteLine("conversion occurred");
               return d.value;
          }
          public static void Main()
          {
               Digit d = new Digit(3);
               // implicit type casting from Digit to byte
                byte b = d;
          }
     }
}

Output
Implicit type casting in C# example

Explicit type casting in C#

A user uses pre-defined functions explicitly to perform explicit type casting.

Here is a list of Type Conversion methods available in C#.
ToBoolean
It converts a type to a Boolean value, where possible.

ToByte
It Converts a type to a byte.

ToChar
This function will Convert a type to a single Unicode character.

ToDateTime
It Converts a type (integer or string type) to date-time structures.

ToDecimal
Used to Convert a floating point or integer type to a decimal type.

ToDouble
It will Convert a type to a double type.

ToInt16
It Converts a type to a 16-bit integer.

ToInt32
This will Convert a type to a 32-bit integer.

ToInt64
It will Convert a type to a 64-bit integer.

ToSbyte
Converts a type to a signed byte type.

ToSingle
It is used to Convert a type to a small floating point number.

ToString
It helps us whenever we want to Convert a type to a string.

ToType
It will help us to Convert a type to a specified type.

ToUInt16
It will Convert a type to an unsigned int type.

ToUInt32
It will Convert a type to an unsigned long type.

ToUInt64
It helps us in Converting a type to an unsigned big integer.

Example Program: –

//Explicit type casting in C# example
using System;
namespace TypeConversionApplication
{
     class StringConversion
     {
          static void Main(string[] args)
          {
               int i = 100;
               float f = 83.005f;
               double d = 12345.7652;
               bool b = true;
               // converting int to string and displaying on cosole
               Console.WriteLine(i.ToString());

               // converting float to string and displaying on cosole
               Console.WriteLine(f.ToString());

               // converting double to string and displaying on cosole
               Console.WriteLine(d.ToString());

               // converting boolean to string and displaying on cosole
               Console.WriteLine(b.ToString());

               Console.ReadKey();
          }
     }
}

Output
Explicit type casting C# example

Leave a Reply

Your email address will not be published. Required fields are marked *