Union types are coming to C# version 15
Posted: (EET/GMT+2)
In the C# language, if you wanted to create methods that return multiple unrelated types based on context, you often had to start using the object base type.
At this writing, Microsoft's C# programming language is in version 14, corresponding to .NET version 10 made available in November 2025. But, planning and implementation for the next C# language version 15 is already in motion, and so, Microsoft has revealed that a union type is coming to the language. Excellent news!
Microsoft shows the following example:
public record class Cat(string Name); public record class Dog(string Name); public record class Bird(string Name); public union Pet(Cat, Dog, Bird);
Notice the new keyword union, which allows the compiler to know that whenever the type Pet is referenced, then it can be any of the three animal record types. Then, you could declare for example a method returning a Pet, but then return a Cat, Dog or Bird type based on method parameters, for example.
If you want to try this out yourself, then you can download the .NET 11 Preview 2, which is now available.
Happy hacking!