extension method
A static method whose first parameter is marked this, so it can be called as though it were an instance method of that type.
In more detail
It adds methods to types you cannot edit. Every AddX and MapX call in ASP.NET Core, such as builder.Services.AddOpenApi(), is an extension method that returns the collection so calls chain.
Example
public static decimal WithVat(this decimal amount, decimal rate) => decimal.Round(amount * (1 + rate), 2);Related terms
- static class — A class that cannot be instantiated and holds only static members, which belong to the type itself rather than to any object.
- IServiceCollection — The list of service registrations the dependency injection container is built from, reached in ASP.NET Core as builder.Services.
- fluent API — An API designed so each call returns an object you can call the next method on, letting configuration read as one chained sentence.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- Building Routes with Minimal APIs — Modern .NET Core, C# & Enterprise Microservices
- Manual Object Mapping — Modern .NET Core, C# & Enterprise Microservices