19 November 2026

C# 15 Union Types for Beginners: Student Admission Results

  Asp.Net CS By Example       19 November 2026

A student application can have three outcomes: accepted, waitlisted, or rejected. A single Boolean cannot describe all three clearly. C# 15 union types let you say that a result is exactly one of a fixed set of types. This beginner example uses a small admissions program to show how a union is created and read.

One result, three possible cases

Score → Decide → Accepted / Waitlisted / Rejected

What is a union type?

A union is a type whose value can be one of several listed case types. Here, AdmissionResult can contain an Accepted, Waitlisted, or Rejected value. Each case has its own useful information. Unlike three unrelated return types, the union gives one clear method return type.

Set up the example

Install the .NET 11 SDK, then run dotnet new console -n UnionAdmissions -f net11.0 and open that folder. The .NET 11 target selects C# 15 by default. Replace Program.cs with this complete example:

// Requires the .NET 11 SDK, which selects C# 15 for net11.0 projects.
foreach (var score in new[] { 92, 68, 45 })
{
    AdmissionResult result = Decide(score);
    Console.WriteLine($"Score {score}: {Describe(result)}");
}

static AdmissionResult Decide(int score)
{
    if (score >= 80) return new Accepted("Seat confirmed");
    if (score >= 60) return new Waitlisted(3);
    return new Rejected("Minimum score is 60");
}

static string Describe(AdmissionResult result) => result switch
{
    Accepted accepted => accepted.Message,
    Waitlisted waitlisted => $"Waiting list position {waitlisted.Position}",
    Rejected rejected => rejected.Reason,
};

public record class Accepted(string Message);
public record class Waitlisted(int Position);
public record class Rejected(string Reason);
public union AdmissionResult(Accepted, Waitlisted, Rejected);

Run dotnet run. The expected lines are Score 92: Seat confirmed, Score 68: Waiting list position 3, and Score 45: Minimum score is 60. The example uses fixed scores so you can focus on the new language feature.

Read the important lines

public union AdmissionResult(Accepted, Waitlisted, Rejected); declares the complete set of cases. Decide returns one case, and C# converts it to the union. The switch in Describe reads that case and chooses the correct message. There is no catch-all arm because every listed case is handled. If you add a fourth case, update the switch too.

When should you use it?

  • Use a union when one result has a small, fixed set of different shapes, such as success, waiting, and failure.
  • Use an ordinary class or record when all values have the same shape.
  • Keep the case types meaningful: Rejected carries a reason, while Waitlisted carries a position.

Practice

  1. Add a NeedsDocuments record and make it a fourth union case. Update Describe.
  2. Change the thresholds and predict all three printed lines before running.
  3. Explain why a single bool would lose information in this example.

Continue with the C# learning path. For the language rules, see Microsoft's C# 15 feature guide and union type reference.

logoblog

Thanks for reading C# 15 Union Types for Beginners: Student Admission Results

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.