Skip to content

Commit f10150b

Browse files
BillWagnerCopilot
andauthored
Add a page for errors in union declarations (#52860)
* Create stubs * Update union-declaration-errors.md * Review the full error list. * Final proofread. * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> Co-authored-by: Bill Wagner <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 32d0b63 commit f10150b

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

docs/csharp/language-reference/compiler-messages/expression-tree-restrictions.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ f1_keywords:
4949
- "CS9226"
5050
- "CS9296"
5151
- "CS9307"
52+
- "CS9369"
5253
helpviewer_keywords:
5354
- "CS0765"
5455
- "CS0831"
@@ -98,7 +99,9 @@ helpviewer_keywords:
9899
- "CS9226"
99100
- "CS9296"
100101
- "CS9307"
101-
ms.date: 05/27/2025
102+
- "CS9369"
103+
ms.date: 04/03/2026
104+
ai-usage: ai-assisted
102105
---
103106
# Resolve errors and warnings generated from expressions prohibited in expression trees
104107

@@ -155,6 +158,7 @@ That's by design. The text closely matches the text of the compiler error / warn
155158
- **CS9226** - *An expression tree may not contain an expanded form of non-array params collection parameter.*
156159
- **CS9296** - *An expression tree may not contain an extension property access*.
157160
- **CS9307** - *An expression tree may not contain a named argument specification out of position*.
161+
- **CS9369** - *An expression tree may not contain a union conversion.*
158162

159163
## Expression tree restrictions
160164

@@ -179,6 +183,7 @@ The following expressions are prohibited:
179183
- Non-destructive mutation using [`with`](../operators/with-expression.md) expressions aren't allowed.
180184
- You can't declare or access [inline arrays](../builtin-types/struct.md#inline-arrays).
181185
- You can't include [collection expressions](../operators/collection-expressions.md).
186+
- [Union conversions](../builtin-types/union.md#union-conversions) aren't allowed.
182187
- The [null propagating](../operators/member-access-operators.md#null-conditional-operators--and-) and [null coalescing](../operators/assignment-operator.md#null-coalescing-assignment) operators aren't allowed.
183188
- [`ref struct`](../builtin-types/ref-struct.md) types, such as <xref:System.Span`1?displayProperty=nameWithType> and <xref:System.ReadOnlySpan`1?displayProperty=nameWithType> aren't allowed.
184189
- `in`, `out`, and `ref` parameters, including `out` variable declarations, aren't allowed.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.

docs/csharp/language-reference/toc.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ items:
502502
record, record struct, record class, positional record,
503503
CS8851, CS8858, CS8859, CS8860, CS8864, CS8865, CS8866, CS8870, CS8872, CS8873,
504504
CS8874, CS8875, CS8877, CS8879, CS8907, CS8908, CS8913
505+
- name: Union type declarations
506+
href: ./compiler-messages/union-declaration-errors.md
507+
displayName: >
508+
union, union type, case type,
509+
CS9370, CS9371, CS9372, CS9373, CS9374, CS9375
505510
- name: Constructor declarations
506511
href: ./compiler-messages/constructor-errors.md
507512
displayName: >
@@ -671,7 +676,7 @@ items:
671676
CS1944, CS1945, CS1946, CS1951, CS1952, CS1963, CS2037, CS7053, CS8072, CS8074,
672677
CS8075, CS8110, CS8122, CS8143, CS8144, CS8153, CS8155, CS8198, CS8207, CS8382,
673678
CS8514, CS8640, CS8642, CS8790, CS8791, CS8792, CS8810, CS8849, CS8927, CS8952,
674-
CS9170, CS9175, CS9307
679+
CS9170, CS9175, CS9307, CS9369
675680
- name: Using directive and aliases
676681
href: ./compiler-messages/using-directive-errors.md
677682
displayName: >

docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,6 @@ f1_keywords:
352352
- "CS9351"
353353
- "CS9352"
354354
- "CS9353"
355-
# Unions
356-
- "CS9369"
357-
- "CS9370"
358-
- "CS9371"
359-
- "CS9372"
360-
- "CS9373"
361-
- "CS9374"
362-
- "CS9375"
363355
helpviewer_keywords:
364356
- "errors [C#], additional information"
365357
---

0 commit comments

Comments
 (0)