C# Type casting

C# type casting is also known as C# type conversion, or converting one type of data to another type. In C#, type casting has two forms:

  1. Implicit type casting (automatic type casting): Basically in this type of casting, C# automatically converts smaller type into larger type, if needed, like int -> float or float -> double
  2. Explicit type casting (manual type casting): In this user provide code to convert larger type into smaller type manually, like double -> float or float to int

Implicit type casting ( Implicit type conversion)

Implicit casting is done automatically when passing a smaller size type to a larger size type, let take a look at an example

int myInt = 20;
double myDouble = myInt;       // Automatic casting: int to double

Console.WriteLine(myInt);      // Outputs 20
Console.WriteLine(myDouble);   // Outputs 20

In the above example, int is automatically converted into double.

For implicit type casting, C# 3.0 also introduced the implicit typed local variable "var".

Var can only be defined in a method as a local variable. The compiler will infer its type based on the value to the right of the "=" operator., for example:

var j = 100; //implicit type casting

You can also check the type of variable using .GetType() method in C#, for example:

using System;

namespace DataTypeCSharp {
   public class Program {
     public static void Main(string[] args) {		 
			var i = 10;
			Console.WriteLine("Type of i is {0}",i.GetType().ToString());

			var str = "Hello World";
			Console.WriteLine("Type of str is {0}", str.GetType().ToString());

			var d = 10.50d;
			Console.WriteLine("Type of d is {0}", d.GetType().ToString());
        
      }
   }
}

Explicit type casting (Explicit type conversion)

Explicit conversions require a cast operator, basically explicit type casting is done manually by placing the type in parentheses in front of the value, for example:

double DoubleValue= 4.78;
int intValue = (int) DoubleValue;    // Manual casting: double to int

Console.WriteLine(DoubleValue);   // Outputs 4.78
Console.WriteLine(intValue);      // Outputs 4

Type Casting Methods

C# provide various type casting methods using which we can do conversions easily, if applied correctly, for example, Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long).

Here is the list of all the built-in type casting methods available in C#

Method Description
Convert.ToBoolean(value) Converts a value to a Boolean value, where possible.
Convert.ToByte(value) Converts a value to a byte.
Convert.ToChar(value) Converts a value to a char.
Convert.ToDecimal(value) Converts a value to decimal type.
Convert.ToDouble(value) Converts a value type to a double type.
Convert.ToInt16(value) Converts a value type to a 16-bit integer.
Convert.ToInt32(value) Converts a type to a 32-bit integer.
Convert.ToInt64(value) Converts a type to a 64-bit integer.
Convert.ToSingle(value) Converts a type to a small floating point number.
Convert.ToString(value) Converts a type to a string.
Convert.ToType(value) Converts a type to a specified type.
Convert.ToDatime(value) Convert a value to datetime type value.

Example:

using System;

namespace DatatypeConversion
{
  public class Program
  {
    public static void Main(string[] args)
    {
      int myInt = 100;
      double myDouble = 2.25;
      bool myBool = true;

      Console.WriteLine(Convert.ToString(myInt));    // Convert int to string
      Console.WriteLine(Convert.ToDouble(myInt));    // Convert int to double
      Console.WriteLine(Convert.ToInt32(myDouble));  // Convert double to int
      Console.WriteLine(Convert.ToString(myBool));   // Convert bool to string
      }
  }
}

Output:

100
100
2
True


Share Tweet