C# is a strongly typed language. In a strongly typed language, you have to specify the type of primitive data objects such as integer, strings, boolean, as well as custom class objects. Often times, you need to convert one data type to another. For instance, you may need to convert a floating data type to an integer and vice versa. Or you may want to store an object of a parent class, in a child class. C# supports two conversion methods: implicit conversion and explicit conversion. In this article, you will see the details of these two conversion types.

Table of Contents

  1. Implicit Conversion
  2. Explicit Conversion

Implicit and Explicit Type Conversion in C#

Implicit Conversion

Implicit conversion is a type of conversion where you do not implicitly have to use any method to convert data from one type to another. Rather C# runtime internally uses typesafe methods to convert from one data type into another.  Implicit conversion is used when you want to convert smaller numeric type data such as integers into larger numeric type data such as float or a double. Implicit conversion can also be used when you want to convert a derived class into a base class object.

Let’s see a simple example of explicit conversion. In the following script, you initialize an integer variable num with the value 35. The script first prints the integer variable on the console, and then stores it in a variable of type double. You can see that no extra method or cast operator is used and you simply assigned an integer value to a double variable. Now,  if you print the value of the double variable num2, you will again see 35 printed on the console.

using System;
namespace DariaApp
{
class Program
{
static void Main(string[] args)
{
int num = 35;
Console.WriteLine(num);
double num2 = num;
Console.WriteLine(num2);
Console.ReadKey();
} 
}
}

Output:

No information is lost when you store smaller numeric variables into a larger numeric variable.

Let’s now see an example of an implicit conversion of a derived or child class object into a parent class object. In the following script, you create two classes: Parent and Child. Both Parent and Child classes contain 1 function which prints a string on the console. The Child class inherits the Parent class. Inside the Main(string[] args) function, you create an object of the Child class and store it in the Parent class variable.

You can see that you can simply assign a Child class object to a Parent class variable without using any conversion method. Finally, you call the Parent class method ParentMethod() using the Parent class variable.

using System;
namespace DariaApp
{
class Parent
{
public void ParentMethod()
{
Console.WriteLine("This is a method inside a parent class");
}
}

class Child : Parent
{
public void ChildMethod()
{
 Console.WriteLine("This is a method inside a child class"); 
}
}

class Program
{
static void Main(string[] args)
{
Parent p = new Child();
p.ParentMethod();
} 
}

}

Output:

Explicit Conversion

In explicit type conversion, you either use a conversion method or a cast operator to convert from one data type to another. You use explicit conversion when you want to convert a larger numeric data type such as a double to an integer or you want to convert a base class object into a derived class object. You also need explicit conversion if you want to convert between two totally unrelated data types e.g. an integer to string or a string to integer etc.

Let’s first see what happens when you try to convert a larger numeric type to a smaller numeric type. The following script initializes a double variable with a value of 35.546. The script then assigns the double variable num to an integer variable num2.

using System;
namespace DariaApp
{

class Program
{
static void Main(string[] args)
{

double num = 35.546;
Console.WriteLine(num);
int num2 = num;
Console.ReadKey();
}
}
}

If you try to run the above script, you will see the following syntax error.

Output:

Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

The above error clearly says that you cannot implicitly convert a double to an integer. The error also hints at using a cast operator.

Let’s now explicitly convert a double type into an integer type. Look at the following script.

using System;
namespace DariaApp
{

class Program
{
static void Main(string[] args)
{

double num = 35.546;
Console.WriteLine(num);
int num2 = Convert.ToInt32(num); //conversion using convert
int num3 = (int)num; // conversion using Cast operator

Console.WriteLine(num2);
Console.WriteLine(num3);
Console.ReadKey();
}
}
}

There are two ways to convert a double type variable into an integer type. You can either use a cast operator (int) or you can use an explicit method Convert.ToInt32(double value). Here is the output of the above script.

Output:

The above output also shows that you can lose information during the explicit conversion. You can see that the decimal part of the value stored by a double variable is lost when you convert a double type into an integer type.

To see a list of all the conversion functions provided by the Convert class, take a look at the official documentation.

Finally, you also need an explicit conversion to convert a base class object into a child or derived class variable. In the following script, you first try to store an object of the Parent class into the Child class variable. However, you will see that you will get a compile-time error while executing the code.

using System;

namespace DariaApp
{
class Parent 
{ 
public void ParentMethod() 
{ 
Console.WriteLine("This is a method inside a parent class"); 
}
}
class Child : Parent 
{ 
public void ChildMethod() 
{ 
Console.WriteLine("This is a method inside a child class"); 
} 
}

class Program
{
static void Main(string[] args)
{
Child c = new Parent();

Console.ReadKey();
}
}
}

Here is the error:

Output:

Cannot implicitly convert type ‘Parent’ to ‘Child’. An explicit conversion exists (are you missing a cast?)

Modify the code inside the Main(string[]args) method of the Program class. Now, you can see that a cast operator converts an object of the Parent class into a Child class.

class Program
{
static void Main(string[] args)
{
Child c = new Child();
Parent p = c;
Child c2 = (Child)p; // casting a base class object into a child class
c2.ChildMethod();
Console.ReadKey();
}
}

When you run the above script, you will see that Child class method ChildMethod() successfully executes. You will see the following output:

Output: