constructor
A special method that runs when an object is created and receives whatever the object needs in order to exist.
In more detail
A class can have several constructors with different parameters. Declaring dependencies as constructor parameters is how a class insists on them, and the dependency injection container reads those parameters to build it.
Related terms
- primary constructor — Constructor parameters written straight after a class name, as in class ProductService(IProductRepository repository), which stay in scope for the whole class body.
- dependency injection — Giving an object the collaborators it needs from outside, usually through its constructor, instead of letting it create or look them up itself.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices