Open sandboxFocus

Property IsUnion

IsUnion

Gets a value indicating whether the type is a union. Also returns false when the type is not a class or a struct.

Declaration
bool IsUnion { get; }
Property Value
Type Description
bool
Remarks

A union is written in one of two forms. The first is a declaration that uses the union keyword. The second is a class or a struct that carries the System.Runtime.CompilerServices.UnionAttribute attribute. This property is true for both, and UnionKind tells them apart.

This property answers whether the type is a union without allocating anything. The authoring form, the cases and the Value property of the union are read through Facets, as in type.Facets.Union?.Cases.

A union is independent of TypeKind. The compiler reports a union declaration as a struct, and the attribute form is a union of the kind that its own declaration gives, so a union is not necessarily a value type.

This property returns false for a type introduced by an aspect. It also returns false when the Metalama engine that runs the aspect cannot read the union, in which case no type is reported as a union. That is the case for the engine built for a Roslyn version that predates C# 15, and, in the current preview, for the engine built for the latest Roslyn version, because the Roslyn member that reports the union is still experimental there.

See Also