are specified in a new proposal of the language committee’s official
C# language repository.
This proposal aims to bring a new level of flexibility and type safety to C#, enabling
developers to write more expressive and resilient code.
Type Unions will probably be added in one of the next releases after C# 13, which is
nearly complete and shipping in November 2024.
What are Type Unions?
Type Unions allow a variable to be of one of several specified types. In essence, a
union type is a type that can represent multiple distinct types. For instance, a union type
int or string means that a variable can hold either an integer or a string value.
This concept is not entirely new and has been successfully implemented in languages like
TypeScript and Kotlin, proving its utility and power.
Why Type Unions Matter
- Enhanced Flexibility: Type Unions enable more flexible function
signatures. For example, a function can accept multiple types for its parameters
without sacrificing type safety. This is particularly useful in scenarios where the
function needs to handle various data types gracefully. - Improved Type Safety: By explicitly stating the possible types a
variable can hold, developers can avoid many common type-related errors. This
clarity helps in maintaining type safety while also providing the flexibility to
work with multiple types. - Better Interoperability: In a world where applications often need
to interact with various data sources and external APIs, Type Unions make it easier
to handle the diverse types of data these interactions can produce. - Cleaner Code: Instead of using complex and verbose type checks and
casts, Type Unions allow for more straightforward and readable code. This reduces
boilerplate and the potential for errors associated with manual type handling.
Standard – Union Classes
A union class is a named type union that declares all its member types in a single
self-contained declaration.
It is declared similar to an enum, except each member is a type that itself can hold state
in one or more state variables.
For each member, only the name and the list of state variables may be
specified.
union U
{
A(int x, string y);
B(int z);
C;
}
Construction:
U u = new A(10, "ten");
Implementation:
A union class is implemented as an abstract record class with the
member types as nested derived record classes.
Specialized – Union Structs (without heap allocation)
Similar to a union class, a union struct is also a named type union that declares all
its member types in a single self-contained declaration,
except the union and the member types are all structs and are able to be used
without heap allocation.
union struct U
{
A(int x, string y);
B(int z);
C;
}
Construction (same as Standard unions):
U u = new A(10, "ten");
Ad Hoc Unions
Ad hoc unions are anonymous unions of types declared elsewhere.
(A or B or C)
(A or B or C) u = new A(10, "ten");
(string or double) value = 10;
Other types of unions added to the proposal are Boxed Unions, Custom Unions and Common Unions
Look ahead
For developers keen on staying at the forefront of C# developments, understanding and
experimenting with Type Unions will be essential. This proposal not only brings C# in
line with other modern languages but also opens up new possibilities for writing
cleaner, safer, and more expressive code.
You can read more on the Type Union features and even the proposed implementation in the
official
C# language repository.
Stay tuned to the official C# language repository for further updates and discussions on
this exciting new feature!
https://github.com/dotnet/csharplang/blob/main/proposals/TypeUnions.md
https://github.com/dotnet/csharplang/issues/113