In C#, a discriminated union is created using the switch statement with pattern matching. It allows defining a type that can hold one of several specified types, with a tag that indicates which one is held.
Here is an example:
main.cs1516 chars52 linesIn this example, we define a base class Shape and several derived classes Square, Circle, and Rectangle. We use an enum ShapeType to tag each shape with a type. Then we define an array of shapes and loop through them, using a switch statement with pattern matching to handle each shape based on its type. Depending on the type of shape, we can access its properties and print out information about it.
gistlibby LogSnag