Type casting in C#

Type casting in C# is a way to convert a value of one data type to another. In C#, there are two types of casting:

  1. Implicit Casting (Automatic) - conversion of a smaller type to a larger type size. C# handles this casting automatically for types like converting an int to a float.
  2. Explicit Casting (Manual) - conversion of a larger type to a smaller size type. C# does not handle this automatically because it can potentially lead to data loss. This requires a cast operator in the form of parentheses with the type that you are casting to.

Let me provide you with an example that demonstrates both implicit and explicit casting:


using System;

class TypeCastingExample
{
    static void Main()
    {
        // Implicit casting (automatically) - no data loss
        int myInt = 9;
        double myDouble = myInt; // An int can be safely cast into a double

        Console.WriteLine("Implicit Casting:");
        Console.WriteLine("Int value: " + myInt); // Outputs 9
        Console.WriteLine("Double value: " + myDouble); // Outputs 9

        // Explicit casting (manually) - possible data loss
        double anotherDouble = 9.78;
        int anotherInt = (int)anotherDouble; // Cast double to int

        Console.WriteLine("\nExplicit Casting:");
        Console.WriteLine("Double value: " + anotherDouble); // Outputs 9.78
        Console.WriteLine("Int value: " + anotherInt); // Outputs 9, not 9.78, because of data loss (decimal part is truncated)

        // Another example of explicit casting
        byte myByte;
        int mySecondInt = 275;
        myByte = (byte)mySecondInt; // Cast int to byte

        Console.WriteLine("\nAnother Explicit Casting:");
        Console.WriteLine("Int value: " + mySecondInt); // Outputs 275
        Console.WriteLine("Byte value: " + myByte); // Outputs 19, because 275 is larger than byte's max value(255) and thus overflows

        // Using Convert class to convert types
        string numberString = "12345";
        int convertedInt = Convert.ToInt32(numberString);

        Console.WriteLine("\nConversion using Convert class:");
        Console.WriteLine("String value: " + numberString); // Outputs "12345"
        Console.WriteLine("Converted Int value: " + convertedInt); // Outputs 12345
    }
}

This program will produce the following output:


Implicit Casting:
Int value: 9
Double value: 9

Explicit Casting:
Double value: 9.78
Int value: 9

Another Explicit Casting:
Int value: 275
Byte value: 19

Conversion using Convert class:
String value: "12345"
Converted Int value: 12345

In the output:

  • The implicit cast did not lose any data because a double can represent all int values.
  • The explicit cast from double to int lost the decimal part, and only the integer part was assigned to anotherInt.
  • The explicit cast from int to byte resulted in overflow because the original int value was outside of the byte range (0-255), demonstrating the potential data loss or unexpected behavior when casting without proper range checks.
  • The Convert class was used to convert a string representation of a number to an int without the need for casting operators.

This example is crafted to explain type casting in C# and should be easy to follow for anyone learning the concept.