In C# 13 and 14, which partial members can a source generator implement, and what must the hand-written declaring declaration look like?
answer
- declaring half without a body
- C# 13 added two, C# 14 two more
- the auto-property look-alike
- initializer only on one half
basics
~20 sMethods have long been partial; C# 13 added properties and indexers, and C# 14 instance constructors and events. The user writes a bodiless declaring declaration with the exact signature, and the generator supplies the body.
solid answer
~40 sA partial member has a declaring declaration without a body and one implementing declaration with it, inside a partial type. Methods have long been partial; C# 13 added properties and indexers, and C# 14 added instance constructors and events. For a property the user writes `public partial string Status { get; set; }`, and the implementation must not be an auto-property, though it may use `field`. A partial constructor's `: base(...)` or `: this(...)` initializer can appear only on the implementing declaration, and a partial event is declared field-like and implemented with `add` and `remove`. Signatures, including accessibility, must match. Only a `void` method with no accessibility modifier, no `out` parameters and no `virtual`-style modifier may go unimplemented; everything else fails with CS8795, CS9248 or CS9275.
code
csharp · 27 linesvar o = new Order(42);
o.Changed += (_, _) => Console.WriteLine("changed");
o.Status = "Shipped";
Console.WriteLine($"{o.Id} {o.Status} {o.Describe()}");
public partial class Order // hand-written half
{
public partial Order(int id); // C# 14
public partial string Status { get; set; } // C# 13
public partial event EventHandler? Changed; // C# 14
public partial string Describe();
public int Id { get; private set; }
}
public partial class Order // the half a generator would emit
{
private EventHandler? _changed;
public partial Order(int id) : this() { Id = id; }
public Order() { }
public partial string Status
{
get => field ?? "Pending";
set { field = value; _changed?.Invoke(this, EventArgs.Empty); }
}
public partial event EventHandler? Changed { add => _changed += value; remove => _changed -= value; }
public partial string Describe() => $"Order #{Id}";
}go deeper
Recall the two halves: a declaration without a body that you write, and a body that a generator or another file supplies.
Explain which members can be partial by C# version and which method shapes may be left unimplemented, with the errors for the rest.
Show the rules a generator must respect: matching signatures, no auto-property implementation, initializers only on the implementing constructor, add and remove on events.
Weigh the language-version floor partial properties, constructors and events impose on a library's users against the cleaner contract they give.
## Partial members are the seams A **partial member** has a **declaring declaration** with no body, written by the user, and an **implementing declaration** with the body, which is exactly what a source generator writes. The user's declaration states the contract (name, signature, accessibility) and the generator honours it. The docs describe partial members as hooks for tooling such as source generators to implement. Every partial member lives inside a partial type; declaring one in a non-partial type fails with **CS0751**. Which members may be partial depends on the language version: | Member | Since | Implementation required? | Error when it is missing | |---|---|---|---| | Method with no accessibility modifier, `void`, no `out` parameters, no `virtual`/`override`/`sealed`/`new`/`extern` | before C# 9 | No: if absent, the declaration and its calls are removed | none | | Any other partial method (for example `public partial string Describe();`) | C# 9 | Yes | CS8795 | | Property or indexer | C# 13 | Yes | CS9248 | | Instance constructor | C# 14 | Yes | CS9275 | | Event | C# 14 | Yes | CS9275 | `partial` is still not allowed on static constructors, finalizers or overloaded operators. ## What each declaring declaration looks like - **Methods**: a signature ending in `;`. Adding any accessibility modifier, even an explicit `private`, turns an optional hook into a required implementation, which is what generator-backed methods want. - **Properties and indexers (C# 13)**: the declaring declaration *looks like* an auto-property, `public partial string Status { get; set; }`. The implementing declaration may not use auto-property syntax: it must implement at least one accessor, and that accessor may be field-backed with the `field` keyword (C# 14). Writing the auto-shaped declaration twice fails with **CS9250** ("A partial property may not have multiple defining declarations, and cannot be an auto-property"). - **Constructors (C# 14)**: `public partial Order(int id);`. There is exactly one defining and one implementing declaration, and **only the implementing declaration may carry a constructor initializer** (`: this(...)` or `: base(...)`). Only one part of the type may use primary-constructor syntax. - **Events (C# 14)**: the defining declaration is **field-like**, `public partial event EventHandler? Changed;`, and the implementing declaration must supply both `add` and `remove` accessors. ## Rules that bind the two halves 1. **Signatures match**: accessibility (including on individual accessors), parameter types and ref kinds, return type and ref kind, and tuple element names. 2. **Some differences are tolerated with warnings**: different parameter names (the declaring names are emitted), a default value written on the implementing declaration, or nullability differences that do not involve oblivious code. 3. **Attributes are combined**, except caller-info attributes: the compiler honours them on the declaring declaration and warns about them on the implementing one. 4. **Documentation comments** come from the implementing declaration when both have them. ## Why the newer members matter to generators Before C# 13 a generator that wanted to own a property had to own the whole property, so the user could not even see it in their own file, or the user wrote a field and the generator wrapped it. Partial properties let the user declare `public partial string Status { get; set; }` in plain sight while a generator implements change notification, validation or a lookup behind it. C# 14 extends the same idea to constructors, for generators that must run set-up when an object is created, and to events, for generators that own subscription storage. ## Mistakes that show up in review - **Putting the generator's half in the user's shape.** A generator that emits `{ get; set; }` for a partial property has written a second declaring declaration, not an implementation, and the user sees CS9250. - **Changing accessibility on one half.** `public partial string Describe();` implemented as `internal partial string Describe()` fails with **CS8799** (both partial member declarations must have identical accessibility modifiers). - **Forgetting the containing types.** A partial member in a nested type needs every enclosing type to be partial as well, since the generator has to re-declare the whole chain. - **Relying on an optional partial method for required behaviour.** If nobody implements `partial void OnSaved();`, the calls vanish silently; use an accessibility modifier when the body must exist. ## Version and tooling notes - On LangVersion 13 a partial constructor or event fails with **CS9260** ("Feature 'partial events and constructors' is not available in C# 13.0"). The SDK picks the language version from the target framework, so a project targeting `net10.0` gets C# 14 by default. - A generator that emits partial properties, constructors or events forces those language versions on its users; a generator can read the consuming project's language version through `ParseOptionsProvider` and fall back to older shapes.
- Why does the compiler reject a second `public partial string Status { get; set; }` as the implementation?Because that shape is reserved for the declaring declaration. With both halves bodiless there is no implementation, so the compiler reports CS9250 (a partial property may not have multiple defining declarations, and cannot be an auto-property) and CS9248 for the missing implementation. The implementing half needs at least one accessor with a body, which may use the `field` keyword for compiler-provided storage.
- What happens to calls to `partial void OnSaved();` when no generator implements it?The compiler removes the declaration and every call to it, including evaluation of the call's arguments. That only works because such a method has no accessibility modifier, returns `void` and has no `out` parameters. Add `private` explicitly and the implementation becomes required, with CS8795 if it is missing.
- Partial methods must always return void and stay private.
- The implementing half of a partial property can be an auto-property.
- Both halves of a partial constructor may chain to base(...).
- A partial event's implementation can stay field-like like its declaration.
- Partial members can live in a non-partial class.
Assumes C# 14 (.NET 10): partial properties and indexers arrived in C# 13, partial constructors and events in C# 14.