liveBook · Manning

Partial method

As explained in ASP.NET Core in Action, 3e

A partial method is a C# method declaration that may be implemented in another part of the same partial type and omitted at compile time when unimplemented.

A partial method is a void-returning C# declaration whose implementation can live in another part of the same partial type. If no implementation is supplied, the compiler removes it. section 11.4.4

This makes partial methods useful as lightweight, optional event handlers. Generated clients can expose hooks that run immediately before a request is sent or after a response is received, while custom behavior stays outside generated code. section 11.4.4

Customizing a generated FruitClient

NSwag marks FruitClient as partial and supplies partial methods as customization points. You can implement a hook in a separate partial class file; leaving it unimplemented adds no runtime event-handler overhead. section 11.4.4

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines implementation placement and compile-time removal, but does not provide declaration syntax or a complete hook implementation.
Examples
Uses NSwag's generated FruitClient and its request-and-response customization hooks.
What you can do after
Apply via C# partial classes to add optional behavior without modifying generated client files.
How it connects
Connects partial methods with partial classes, generated clients, NSwag, and event handlers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.