|
| 1 | +--- |
| 2 | +title: "Resolve errors and warnings related to union type declarations" |
| 3 | +description: "This article helps you diagnose and correct compiler errors and warnings related to union type declarations." |
| 4 | +f1_keywords: |
| 5 | + - "CS9370" |
| 6 | + - "CS9371" |
| 7 | + - "CS9372" |
| 8 | + - "CS9373" |
| 9 | + - "CS9374" |
| 10 | + - "CS9375" |
| 11 | +helpviewer_keywords: |
| 12 | + - "CS9370" |
| 13 | + - "CS9371" |
| 14 | + - "CS9372" |
| 15 | + - "CS9373" |
| 16 | + - "CS9374" |
| 17 | + - "CS9375" |
| 18 | +ms.date: 04/03/2026 |
| 19 | +ai-usage: ai-assisted |
| 20 | +--- |
| 21 | +# Resolve errors and warnings for union type declarations |
| 22 | + |
| 23 | +The C# compiler generates errors when you misuse [union types](../builtin-types/union.md). Union types represent a value that can be one of several *case types*, with implicit conversions and exhaustive pattern matching. These diagnostics help you follow the rules for declaring and using union types. |
| 24 | + |
| 25 | +<!-- The text in this list generates issues for Acrolinx, because they don't use contractions. |
| 26 | +That's by design. The text closely matches the text of the compiler error / warning for SEO purposes. |
| 27 | + --> |
| 28 | + |
| 29 | +- [**CS9370**](#union-declaration-requirements): *A union declaration must specify at least one case type.* |
| 30 | +- [**CS9371**](#union-declaration-requirements): *Cannot convert type to 'object' via an implicit reference or boxing conversion.* |
| 31 | +- [**CS9372**](#pattern-matching-limitations): *An expression of type cannot be handled by this pattern, see additional errors at this location.* |
| 32 | +- [**CS9373**](#union-member-restrictions): *Instance fields, auto-properties, or field-like events are not permitted in a 'union' declaration.* |
| 33 | +- [**CS9374**](#union-member-restrictions): *Explicitly declared public constructors with a single parameter are not permitted in a 'union' declaration.* |
| 34 | +- [**CS9375**](#union-member-restrictions): *A constructor declared in a 'union' declaration must have a 'this' initializer that calls a synthesized constructor or an explicitly declared constructor.* |
| 35 | + |
| 36 | +## Union declaration requirements |
| 37 | + |
| 38 | +- **CS9370**: *A union declaration must specify at least one case type.* |
| 39 | +- **CS9371**: *Cannot convert type to 'object' via an implicit reference or boxing conversion.* |
| 40 | + |
| 41 | +A [union declaration](../builtin-types/union.md#union-declarations) specifies a name and a list of case types. These errors enforce the structural requirements of a valid union declaration. For the complete rules, see the [union types feature specification](~/_csharplang/proposals/unions.md). |
| 42 | + |
| 43 | +To correct these errors, apply the following changes to your union declaration: |
| 44 | + |
| 45 | +- Add at least one case type to the union's type list (**CS9370**). A union must have one or more case types because the union's purpose is to represent a closed set of alternatives. A declaration like `union Empty()` with no case types isn't meaningful. |
| 46 | +- Change or remove case types that don't convert to `object` via an implicit reference or boxing conversion (**CS9371**). Union types store their contents as a single `object?` reference, so every case type must be convertible to `object`. Types like pointers or `ref struct` types don't satisfy this requirement. Replace them with a wrapper type that can be boxed or stored as a reference. |
| 47 | + |
| 48 | +## Union member restrictions |
| 49 | + |
| 50 | +- **CS9373**: *Instance fields, auto-properties, or field-like events are not permitted in a 'union' declaration.* |
| 51 | +- **CS9374**: *Explicitly declared public constructors with a single parameter are not permitted in a 'union' declaration.* |
| 52 | +- **CS9375**: *A constructor declared in a 'union' declaration must have a 'this' initializer that calls a synthesized constructor or an explicitly declared constructor.* |
| 53 | + |
| 54 | +A [union declaration](../builtin-types/union.md#union-declarations) can include a body with additional members, but the compiler restricts what you can declare. These restrictions ensure that the compiler-generated conversion constructors and storage mechanism work correctly. For the complete rules, see the [union types feature specification](~/_csharplang/proposals/unions.md). |
| 55 | + |
| 56 | +To correct these errors, apply the following changes to your union members: |
| 57 | + |
| 58 | +- Remove instance fields, auto-implemented properties, and field-like events from the union body (**CS9373**). A union doesn't define new data members. It composes existing types into a closed set of alternatives. Declare any additional state in the case types themselves or use static members on the union. |
| 59 | +- Remove or change the accessibility of any explicitly declared public constructor that takes a single parameter (**CS9374**). The compiler generates a public single-parameter constructor for each case type to support implicit union conversions. An explicit constructor with the same shape conflicts with those generated constructors. If you need a single-parameter constructor, make it `internal` or `private`. |
| 60 | +- Add a `this` initializer to any explicitly declared constructor so that it chains to a synthesized constructor or another explicitly declared constructor (**CS9375**). The compiler-generated constructors initialize the union's internal storage correctly. Constructors that don't chain through them might leave the union in an invalid state. Use `: this(someValue)` to chain to a generated case-type constructor, or `: this()` to chain to an explicit parameterless constructor you declare. |
| 61 | + |
| 62 | +## Pattern matching limitations |
| 63 | + |
| 64 | +- **CS9372**: *An expression of type cannot be handled by this pattern, see additional errors at this location.* |
| 65 | + |
| 66 | +This error arises when you use an incorrect pattern form with a union type. For the complete rules on union pattern matching, see [union matching](../builtin-types/union.md#union-matching). |
| 67 | + |
| 68 | +To correct this error, use the correct pattern form when matching against a union value (**CS9372**). Patterns on a union apply to the union's `Value` property, not the union value itself. If the compiler reports that a pattern can't handle the expression, check that you're matching against the case types listed in the union declaration. Review the additional errors at the same location for details about which pattern is invalid. |
0 commit comments