C# switch on type

C# switch on type

In C#, you can use the switch statement to switch on the type of an object. This is called a "type switch". Here's an example:

object obj = "hello world";

switch (obj)
{
    case int i:
        Console.WriteLine($"Integer: {i}");
        break;
    case string s:
        Console.WriteLine($"String: {s}");
        break;
    case double d:
        Console.WriteLine($"Double: {d}");
        break;
    default:
        Console.WriteLine("Unknown type");
        break;
}

In this example, the switch statement is used to switch on the type of the obj variable. The case statements specify the types that we want to match on, along with a variable name that we want to use to refer to the object if the type matches.

In this example, the obj variable is a string, so the second case statement matches and the s variable is assigned the value of the obj variable. The corresponding code block is executed, which outputs "String: hello world".

If the type of the object doesn't match any of the cases, the default case is executed. In this example, the default case outputs "Unknown type".

Note that the type switch is only available in C# 7.0 and later versions. If you're using an earlier version of C#, you can achieve similar functionality using if statements and the is operator.

Examples

  1. "C# switch on type with specific class"

    • Code:
      object obj = new MyClass();
      
      switch (obj)
      {
          case MyClass myClass:
              Console.WriteLine("It's an instance of MyClass");
              break;
      
          case YourClass yourClass:
              Console.WriteLine("It's an instance of YourClass");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Demonstrates using switch on the type of an object, identifying whether it's an instance of a specific class (MyClass or YourClass).
  2. "C# switch on type with primitive types"

    • Code:
      object value = 42;
      
      switch (value)
      {
          case int intValue:
              Console.WriteLine($"It's an integer: {intValue}");
              break;
      
          case string stringValue:
              Console.WriteLine($"It's a string: {stringValue}");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Illustrates using switch to identify the type of a variable (int or string) and perform different actions based on the type.
  3. "C# switch on type with interface"

    • Code:
      IShape shape = new Circle();
      
      switch (shape)
      {
          case Circle circle:
              Console.WriteLine($"It's a Circle with radius {circle.Radius}");
              break;
      
          case Rectangle rectangle:
              Console.WriteLine($"It's a Rectangle with width {rectangle.Width} and height {rectangle.Height}");
              break;
      
          default:
              Console.WriteLine("Unknown shape type");
              break;
      }
      
    • Description: Shows using switch on the type of an object implementing an interface (IShape in this case) and identifying specific implementations (Circle or Rectangle).
  4. "C# switch on type with nullable types"

    • Code:
      object obj = 42;
      
      switch (obj)
      {
          case int? nullableInt:
              Console.WriteLine($"It's a nullable integer: {nullableInt}");
              break;
      
          case double? nullableDouble:
              Console.WriteLine($"It's a nullable double: {nullableDouble}");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Demonstrates using switch to handle nullable types (int? or double?) and perform different actions based on the type.
  5. "C# switch on type with pattern matching"

    • Code:
      object obj = "Hello";
      
      switch (obj)
      {
          case string stringValue when stringValue.Length > 5:
              Console.WriteLine($"Long string: {stringValue}");
              break;
      
          case int intValue:
              Console.WriteLine($"Integer: {intValue}");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Shows using pattern matching in switch to handle types and apply additional conditions based on the matched type.
  6. "C# switch on type with generics"

    • Code:
      T GenericSwitch<T>(T value)
      {
          switch (value)
          {
              case int intValue:
                  Console.WriteLine($"It's an integer: {intValue}");
                  break;
      
              case string stringValue:
                  Console.WriteLine($"It's a string: {stringValue}");
                  break;
      
              default:
                  Console.WriteLine("Unknown type");
                  break;
          }
      
          return value;
      }
      
    • Description: Demonstrates using switch on the type of a generic parameter, allowing the method to handle various types in a generic way.
  7. "C# switch on type with multiple interfaces"

    • Code:
      object obj = new ClassWithInterfaces();
      
      switch (obj)
      {
          case IInterface1 i1:
              Console.WriteLine("Implements IInterface1");
              break;
      
          case IInterface2 i2:
              Console.WriteLine("Implements IInterface2");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Illustrates using switch on the type of an object that implements multiple interfaces (IInterface1 or IInterface2) and handling each case accordingly.
  8. "C# switch on type with custom type hierarchy"

    • Code:
      Animal animal = new Cat();
      
      switch (animal)
      {
          case Dog dog:
              Console.WriteLine($"It's a Dog named {dog.Name}");
              break;
      
          case Cat cat:
              Console.WriteLine($"It's a Cat named {cat.Name}");
              break;
      
          default:
              Console.WriteLine("Unknown animal type");
              break;
      }
      
    • Description: Demonstrates using switch on the type of objects within a custom type hierarchy (Dog or Cat in this example).
  9. "C# switch on type with nested switch statements"

    • Code:
      object obj = 42;
      
      switch (obj)
      {
          case int intValue:
              switch (intValue)
              {
                  case 1:
                      Console.WriteLine("Nested switch: Case 1");
                      break;
      
                  case 2:
                      Console.WriteLine("Nested switch: Case 2");
                      break;
      
                  default:
                      Console.WriteLine("Nested switch: Default Case");
                      break;
              }
              break;
      
          default:
              Console.WriteLine("Outer switch: Unknown type");
              break;
      }
      
    • Description: Shows using nested switch statements to handle types and perform additional checks based on the inner switch.
  10. "C# switch on type with is pattern matching"

    • Code:
      object obj = "Hello";
      
      switch (obj)
      {
          case var stringValue when stringValue is string:
              Console.WriteLine($"It's a string: {stringValue}");
              break;
      
          case var intValue when intValue is int:
              Console.WriteLine($"It's an integer: {intValue}");
              break;
      
          default:
              Console.WriteLine("Unknown type");
              break;
      }
      
    • Description: Demonstrates using switch with the is pattern matching to handle types and execute code based on the type.

More Tags

subscribe binary-search-tree oauth-2.0 homekit mapi train-test-split remote-connection autocompletetextview command serilog

More C# Questions

More Mixtures and solutions Calculators

More Electrochemistry Calculators

More Genetics Calculators

More Retirement Calculators