Skip to content

Clarify usage of IUnionMembers interface - #55183

Open
BarionLP wants to merge 2 commits into
dotnet:mainfrom
BarionLP:main
Open

Clarify usage of IUnionMembers interface #55183
BarionLP wants to merge 2 commits into
dotnet:mainfrom
BarionLP:main

Conversation

@BarionLP

@BarionLP BarionLP commented Jul 29, 2026

Copy link
Copy Markdown

Summary

clarify that all union members must be part of the interface
so far the paragraph mostly talks about factory methods.


Internal previews

File Preview link
docs/csharp/language-reference/builtin-types/union.md docs/csharp/language-reference/builtin-types/union

clarify Union member providers
@BarionLP
BarionLP requested review from a team and BillWagner as code owners July 29, 2026 16:29
@dotnetrepoman dotnetrepoman Bot added this to the July 2026 milestone Jul 29, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates PR is created by someone from the .NET community. label Jul 29, 2026

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BarionLP

This is a good start. I'd like to see the example updated to include the additional members. So, instead of:

[System.Runtime.CompilerServices.Union]
public record class Outcome<T> : Outcome<T>.IUnionMembers
{
    private readonly object? _value;

    private Outcome(object? value) => _value = value;

    public interface IUnionMembers
    {
        static Outcome<T> Create(T? value) => new(value);
        static Outcome<T> Create(Exception? value) => new(value);
        object? Value { get; }
    }

    object? IUnionMembers.Value => _value;
}

The sample should include:

[System.Runtime.CompilerServices.Union]
public record class Outcome<T> : Outcome<T>.IUnionMembers
{
    private readonly object? _value;

    private Outcome(object? value) => _value = value;

    public interface IUnionMembers
    {
        static Outcome<T> Create(T? value) => new(value);
        static Outcome<T> Create(Exception? value) => new(value);
        object? Value { get; }

        bool TryGetValue(out T value);
        bool TryGetValue(out Exception value);
    }

    object? IUnionMembers.Value => _value;
}

Can you add that as well?

Comment on lines +174 to 177
> [!NOTE]
> If an `IUnionMembers` interface is used, all union members must be defined on that interface.

A union type can delegate its union members to a nested `IUnionMembers` interface. When this interface is present, the compiler looks for `Create` factory methods instead of constructors:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be a note. Instead, I'd flow it into the regular text, based on Alexsey's comments on the runtime issue:

Suggested change
> [!NOTE]
> If an `IUnionMembers` interface is used, all union members must be defined on that interface.
A union type can delegate its union members to a nested `IUnionMembers` interface. When this interface is present, the compiler looks for `Create` factory methods instead of constructors:
A union type can delegate its union members to a nested `IUnionMembers` interface. When this interface is present, the `union` type behaves as a *union member provider*. The compiler generates code to call the `IUnionMembers` interface. It won't generate calls to members declared on the union type that aren't members of the nested `IUnionMembers` interface. As the following example shows, that means you must add the necessary factory methods and the appropriate `TryGetValue` methods for all case types:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants