Mastering C# Union Types: A Comprehensive Guide
In the ever-evolving landscape of programming, C# continues to empower developers with its robust features and innovative solutions. One such feature that has gained significant traction is the introduction of union types in C# 9.0 and later versions. Union types, also known as discriminated unions, are a powerful tool that enables you to express a value that can be one of several different types. Let's delve into the world of C# union types, exploring their syntax, benefits, and best practices.
Understanding C# Union Types
At its core, a union type in C# is a type that can hold a value of any of its declared types. It's a way to express a sum type, where the type system knows that a value can be one of several different types. This concept is not new; it's been around in functional programming languages like F# and Haskell for years. However, its inclusion in C# 9.0 has opened up new possibilities for type-safe and expressive code.
Syntax of Union Types
Here's a simple example of how to declare a union type in C#:

public record Person(string Name, int Age);
public record Address(string Street, string City);
public record PersonOrAddress(Person Person, bool IsPerson)
: PersonOrAddress(Address Address, bool IsAddress);
In this example, `PersonOrAddress` is a union type that can hold either a `Person` or an `Address`. The boolean flag (`IsPerson` or `IsAddress`) is used to discriminate between the two types.
Benefits of Using Union Types in C#
- Expressive Code: Union types allow you to write code that is more self-describing and easier to understand.
- Type Safety: Unlike nullables or object types, union types ensure that you're working with the correct type at compile time.
- Pattern Matching: Union types work seamlessly with C#'s pattern matching feature, enabling more concise and readable switch statements.
Working with Union Types
Pattern Matching with Union Types
Pattern matching is a powerful feature in C# that allows you to test a value against a pattern and bind variables in a single expression. Here's how you can use pattern matching with union types:
public void PrintDetails(PersonOrAddress personOrAddress)
{
switch (personOrAddress)
{
case PersonOrAddress(Person p, true):
Console.WriteLine($"Name: {p.Name}, Age: {p.Age}");
break;
case PersonOrAddress(Address a, false):
Console.WriteLine($"Street: {a.Street}, City: {a.City}");
break;
}
}
Union Types and Nullability
Union types can also be used to express nullability in a type-safe way. Here's an example:

public record Person(string Name, int Age);
public record Address(string Street, string City);
public record PersonOrAddress(Person? Person, bool IsPerson)
: PersonOrAddress(Address? Address, bool IsAddress);
In this example, `Person` and `Address` can be null, but the union type ensures that only one of them can be non-null at a time.
Best Practices with Union Types
- Keep it Simple: Start with small, simple union types. As you get more comfortable, you can create more complex ones.
- Use Discriminators: Always use a discriminator (like `IsPerson` or `IsAddress`) to distinguish between the types in your union.
- Favor Records: Union types are typically used with records. Records provide a concise syntax and automatically generate useful methods.
Conclusion
C# union types are a powerful feature that can help you write more expressive, type-safe code. Whether you're working with nullables, sum types, or complex data structures, union types provide a robust and flexible solution. As with any new feature, it takes time to understand and integrate union types into your coding practices. But with patience and practice, you'll find that union types can greatly enhance your coding experience in C#.























