Dependency injection in .NET
Answer: .NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern. (S1)
The problem it solves
- A dependency is an object that another object depends on. (S1)
- When a Worker class creates and directly depends on a MessageWriter class, replacing MessageWriter with a different implementation means you must modify the Worker class. (S1)
- In a large project with multiple classes depending on MessageWriter, the configuration code becomes scattered across the app. (S1)
- Dependency injection is a design pattern that you can use to remove hard-coded dependencies and make your application more maintainable and testable. (S2)
The moving parts
- IServiceCollection defines a contract for a collection of service descriptors. (S2)
- IServiceProvider defines a mechanism for retrieving a service object. (S2)
- ServiceDescriptor describes a service with its service type, implementation, and lifetime. (S2)
- Services are typically registered at the app's start-up and appended to an IServiceCollection. (S1)
- After you register services, call the BuildServiceProvider method to build an IServiceProvider instance, which acts as a container for all the registered services. (S2)
- The AddSingleton method registers the service with a singleton lifetime, which means it isn't disposed until the app shuts down. (S2)
Claims
- id: C1 claim: DI is a technique for achieving Inversion of Control. source: S1 evidence: "which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies."
- id: C2 claim: A dependency is an object another object depends on. source: S1 evidence: "A dependency is an object that another object depends on."
- id: C3 claim: IServiceProvider retrieves service objects. source: S2 evidence: "IServiceProvider: Defines a mechanism for retrieving a service object."
- id: C4 claim: BuildServiceProvider builds an IServiceProvider. source: S2 evidence: "call the BuildServiceProvider method to build an IServiceProvider instance."
Sources
- [S1] Dependency injection - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Dependency injection basics quickstart - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-basics . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Dependency injection - .NET, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Dependency injection basics quickstart - .NET, CC BY 4.0 (dotnet/docs).
Cite this
iinventedlight. 2026. "Dependency injection in .NET." iinventedlight Library, iil:note:ejjyzwz7, version of 2026-11-04. https://iinventedlight.com/library/notes/dotnet-dependency-injection/. License: CC-BY-4.0.
@misc{iil_note_ejjyzwz7,
author = {{iinventedlight}},
title = {{Dependency injection in .NET}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/dotnet-dependency-injection/},
note = {Permanent ID iil:note:ejjyzwz7. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/dotnet-dependency-injection.md.