Write A Program To Demonstrate Type Casting In C#

Do you want to know about type casting in C#? In this tutorial, I will explain how to write a program to demonstrate type casting in C#.

To demonstrate type casting in C#, you can use either implicit or explicit casting. Implicit casting automatically converts a smaller data type to a larger one (e.g., int to long), while explicit casting is necessary when converting a larger data type to a smaller one or between incompatible types (e.g., double to int using (int)myDouble).

What is Type Casting in C#?

Type casting is the process of converting a variable from one data type to another. In C#, this can be necessary when you need to manipulate variables in ways that require them to be of a different data type. There are two main types of type casting:

  1. Implicit Casting (Automatic): This happens when the conversion is safe and no data will be lost. For example, converting an int to a long.
  2. Explicit Casting (Manual): This is required when there’s a risk of data loss, or when the conversion might not be safe. For example, converting a double to an int.

Why is Type Casting Important in C#?

Type casting is essential for several reasons:

  • Flexibility in Programming: It allows for greater flexibility in your code, enabling you to work with different data types more effectively.
  • Control and Precision: It gives you control over how your data is treated and ensures that you can manage the precision of your computations.
  • Interoperability with Different Systems: Sometimes, when interfacing with different systems or libraries, you need to convert data types to match external requirements.

Demonstrating Type Casting in C#

Now let’s dive into some examples to demonstrate both implicit and explicit type casting in C#.

Example 1: Implicit Casting

using System;

class Program
{
    static void Main()
    {
        int myInt = 9;
        long myLong = myInt;  // Implicit casting from int to long

        Console.WriteLine("Implicitly Casted long: " + myLong);
    }
}

In this example, myInt is an integer which is implicitly cast to a long integer myLong. This is a safe conversion since a long can hold all values of an int.

Here, you can see the output in the below screenshot.

write a program to demonstrate type casting in c#

Example 2: Explicit Casting

using System;

class Program
{
    static void Main()
    {
        double myDouble = 9.78;
        int myInt = (int)myDouble;  // Explicit casting from double to int

        Console.WriteLine("Explicitly Casted int: " + myInt);
    }
}

Here, myDouble is a double precision floating-point number. To convert it into an int, explicit casting is required. Note that this conversion might lead to data loss as the fractional part of the double value is truncated.

You can see the output after I ran the code using the Visual Studio console application.

type casting in c#

Handling Casting Exceptions

When performing explicit casts, especially from a larger to a smaller data type or from a different kind of data type (like from string to int), it’s important to handle potential exceptions to avoid runtime errors. Here’s an example:

using System;

class Program
{
    static void Main()
    {
        try
        {
            string stringValue = "12345";
            int intValue = Convert.ToInt32(stringValue);
            Console.WriteLine("Converted int: " + intValue);
        }
        catch (FormatException e)
        {
            Console.WriteLine("Format Exception: " + e.Message);
        }
        catch (OverflowException e)
        {
            Console.WriteLine("Overflow Exception: " + e.Message);
        }
    }
}

Once you run the code using a console application, you can see the output in the screenshot below:

program to demonstrate type casting in c#

Conclusion

Type casting in C# is very powerful and it allows for effective manipulation of data types, ensuring that your code can interact seamlessly with different types of data. In this C# tutorial, I have explained how to write a program to demonstrate type casting in c#.

You may also like: