c sharp

How To Switch On Type in C#

C# is a modern programming language that supports many features and constructs to make programming more efficient and effective. One of these features is type switching, which allows a developer to execute code conditionally based on the runtime type of object. In C#, type switching is achieved through the ‘switch’ statement, which can be used to match a variable against a set of possible types.

How to Switch on Type in C#

C# does not support the switch-on type, but it can be simulated using the switch statement. The ‘switch’ statement in C# is typically used to execute different code blocks depending on the value of a variable. However, it can also be used to switch on types, enabling a developer to execute specific code blocks depending on the type of object. This feature can especially be useful in cases where you need to perform different operations on objects of different types, without having to use ‘if-else’ statements.

To switch on types in C#, you need to use the ‘switch’ statement in combination with the ‘case’ keywords. The ‘case’ keyword is used to specify the type you want to match against, Here is an example of how to use type switching in C#:

using System;

class Program
{
    static void Main(string[] args)
    {
        object obj = 10;

        switch (obj)
        {
            case int i:
                Console.WriteLine("The object is an integer.");
                break;
            case string s:
                Console.WriteLine("The object is a string.");
                break;
            default:
                Console.WriteLine("The object is of an unknown type.");
                break;
        }
    }
}

This C# code demonstrates how to use the switch statement to perform type-checking in C#. First, the code initializes an object called “obj” with the integer value 10. Then, the switch statement is used to check the type of the object. In this case, the switch statement has three cases: int, string, and default. If the object is an integer, the code will print “The object is an integer.” If the object is a string, the code will print “The object is a string.” If the object is neither an integer nor a string, the code will print “The object is of an unknown type.” This code is useful for dynamically checking the type of object at runtime, allowing the code to handle different types of data in different ways.

Conclusion

Type switching is a useful feature in C# that allows developers to conditionally execute code based on the runtime type of object. This feature can be achieved through the ‘switch’ statement, which can be used in combination with the ‘case’ and ‘is’ keywords. By using type switching, developers can write more concise and efficient code, avoiding the need for lengthy ‘if-else’ statements to test for different types of objects.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.