Type Casting in C#

Last Updated : 20 Apr, 2026

Type casting means converting a variable of one data type into another, ensuring compatibility when assigning data between different types.

For Example:

C#
using System;
class Program
{
    static void Main()
    {
        int num = 10;

        // int is converted to double (implicit type casting)
        double d = num;

        Console.WriteLine(d);

    }
}

Output
10

Type Casting can be divided into two parts:

  • Implicit Type Conversion (Type Safe)
  • Explicit Type Conversion (Manual Conversion)

1. Implicit Type Casting

Implicit type casting (type-safe casting) is the process in which the C# compiler automatically converts a smaller (compatible) data type into a larger data type (e.g., int to double) without any data loss. It is performed automatically by the compiler.

implicit
Implicit Type casting conditions

In C#, numeric types support implicit conversions among themselves, but not to char or bool. Also, char and bool are incompatible. The compiler ensures type compatibility before conversion.

C#
using System;

class Program
{
    static void Main()
    {
        int x = 10;
        double d = x;  
        // implicit casting

        Console.WriteLine(d);
    }
}

Output
10

Invalid Implicit Conversion

C#
int x = 10;
double d = 9.5;

// Error
x = d;

Error: Cannot implicitly convert double to int.

2. Explicit Type Casting

Explicit type casting is done manually using a cast operator. It is required when converting a larger data type into a smaller one and may cause data loss. Let's take a look at an example:

explicit
Explicit Type Casting

Example: Using Explicit Conversion to convert double into int.

C#
using System;

class Program
{
    static void Main()
    {
        double d = 9.8;
        
        // explicit casting
        int x = (int)d;  

        Console.WriteLine(x);
    }
}

Output
9

This solves the limitation of implicit casting by using explicit casting. Additionally, we can use built-in methods for more flexible type conversions.

MethodDescription
ToBoolean()Converts value to Boolean
ToChar()Converts value to character
ToByte()Converts value to byte
ToDecimal()Converts value to decimal
ToDouble()Converts value to double
ToInt16()Converts value to 16-bit integer
ToInt32()Converts value to 32-bit integer
ToInt64()Converts value to 64-bit integer
ToString()Converts value to string
ToUInt16()Converts value to unsigned 16-bit integer
ToUInt32()Converts value to unsigned 32-bit integer
ToUInt64()Converts value to unsigned 64-bit integer

Type Conversion Methods

There are some built in methods defined for type conversions mentioned below:

Example: Typecasting using built-in methods

C#
using System;

namespace Casting {
class Geeks {

    public static void Main(String[] args)
    {
        int i = 12;
        double d = 765.12;
        float f = 56.123F;

        // Using Built- In Type Conversion Methods & Displaying Result
        Console.WriteLine(Convert.ToString(f));
        Console.WriteLine(Convert.ToInt32(d));
        Console.WriteLine(Convert.ToUInt32(f));
        Console.WriteLine(Convert.ToDouble(i));
        Console.WriteLine("GeeksforGeeks");
    }
}
}

Output
56.123
765
56
12
GeeksforGeeks
Comment

Explore