Table of contents
1.
Introduction
2.
Types of Conversion
2.1.
Implicit Conversion
2.2.
Explicit Conversion
2.3.
Type Conversion methods or using Convert class
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

C# Type Conversion

Author Ankit Kumar
2 upvotes

Introduction

If the variable type is known at compile-time, the language is statically typed. And C# is one of those languages. Since C# is a statically typed language, a variable cannot be declared again or assigned a value of a different type once it has been defined and assigned. There is a solution—type Casting. 

When the variable type changes according to our needs, it is called typecasting. 

When the variable of one data type is changed to another data type is known as Type Casting. According to our needs, we can change the type of data. 

We are now moving to the types of conversion.

Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.

Types of Conversion

Implicit Conversion

When the conversion is done automatically, it is called an implicit conversion. In this conversion, no data is lost, so it's also the safest conversion. Since this is automatic conversion, no syntax is required. 

In the case of data types, variables are upgraded into the largest data type, which happens to be present in the expression. It happens when the variable on LHS is greater than the variable on RHS.

Convert from Data Type Convert to Data Type
byte short, int, long, float, double
short int, long, float, double
int long, float, double
long float, double
float double

Let’s understand with the following code.

We'll start as usual with the Hello world string. Then declare an integer. After that, we'll declare a double and in which we'll initialise with the first integer. Here it'll automatically get casted, and then we'll have both variable outputs. Then we'll verify the conversion by checking the type of variable. 

using System; 
public class CodingNinjas
{
    public static void Main(string[] args)
    {
        Console.WriteLine ("Hello World");
        int Test1 = 9;
        // Automatic casting: int to double
        double Test2 = Test1;
        // Produces Output as 9
        Console.WriteLine(Test1);
        // Produces Output as 9
        Console.WriteLine(Test2);      
        // Produces Output as System.Double
        Console.WriteLine(Test2.GetType());   
    }
}

Output - 

Hello World

9

9

System.Double

 

Now, we have to move to the next type.

Explicit Conversion

There may be times when the implicit conversion fails. Maybe the conversion fails, or there may be a risk of data loss. Or, due to some complexity, the implicit conversion fails, then explicit conversion is the way. The process of explicit conversion is also called a cast. 

For the explicit conversion, we need to specify the type in which we want to do the casting in parentheses in front of the value or variable to be converted. 

Let’s understand the concept further with the following code:

We'll declare a double variable and then initialise the same. Then we'll explicitly typecast the double variable into the int variable.

using System; 
public class CodingNinjas
{
    public static void Main(string[] args)
    {
      double Test = 199.7;
        int Test1;
       Test1=(int)Test; 
        // Manually casting double to int.
        System.Console.WriteLine(Test1);
    }
}

Output:

199

It’s time to move to the next method.

Type Conversion methods or using Convert class

There are some built-in commands to convert data explicitly. First of all, we’ll see the code.

We’ll start with the declaration and initialisation of the variables,i.e. Int, double and bool. Then we will typecast the variables using the in-built commands. Int to string and then int to double. Then we’ll change double to int. And at the end bool to string.

using System;
public class CodingNnjas
{
    public static void Main(string[] args)
    {
int East = 10;
double West = 5.25;
bool North = true;
// convert int to string 
Console.WriteLine(Convert.ToString(East));
 // convert int to double  
Console.WriteLine(Convert.ToDouble(East));
 // convert double to int   
Console.WriteLine(Convert.ToInt32(West));
// convert bool to string 
Console.WriteLine(Convert.ToString(North));   
     }
}

Output -

10

10

5

True

Following are the built-in methods provided by C# for Explicit Type-Conversions:

From To
ToBoolean It will convert a type to the Boolean value
ToChar It will convert a type to a character value
ToByte It will convert a value to Byte Value
ToDecimal It will convert a value to a Decimal point value
ToDouble It will convert a type to double data type
ToInt16 It will convert a type to a 16-bit integer
ToInt32 It will convert a type to a 32-bit integer
ToInt64 It will convert a type to a 64-bit integer
ToString It will convert a given type to a string
ToUInt16 It will convert a type to an unsigned 16-bit integer
ToUInt32 It will convert a type to an unsigned 32-bit integer
ToUInt64 It will convert a type to an unsigned 64-bit integer

Let us now discuss some of the frequently asked questions related to the topic.

FAQs

  1. What is typecasting?
    When the type of variable is changed according to our needs, it is called typecasting.
  2. What is implicit conversion?
    When the conversion is done automatically, it is called an implicit conversion. In this conversion, no data is lost, so it's also the safest conversion. Since this is automatic conversion, no syntax is required.
  3. What is explicit conversion?
    There may be times when the implicit conversion fails. Maybe the conversion fails, or there may be a risk of data loss. When the type is changed manually by command, then it is known as explicit conversion.
  4. What is conversion class methods?
    Conversion class methods are the predefined commands to cast any variable of a data type into another desired data type.
  5. Describe five conversion class methods.
    Following are the five conversion class methods.

ToBoolean - It will convert a type to the Boolean value

ToChar - It will convert a type to a character value

ToByte - ​​It will convert a value to Byte Value

ToDecimal - It will convert a value to a Decimal point value

ToDouble - It will convert a type to double data type

Now let’s summarise the learning.

Key Takeaways

In this article, we have discussed the type conversion in C#. We started with the need for type conversion, and then we learned about typecasting. We learned about different types of typecasting. We also went through some examples. In the end, we learned some of the FAQs of the topic. 

We hope that this blog has helped you enhance your knowledge regarding Type conversion in C# and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass