Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pankajkumar PatelRank #181 · 13.4K reputationPosted Aug 18, 2026, 2:34 AM EDT
Hi Boobalan Thiyagarajan,
Dependency Injection (DI) in C# is a design pattern where one passes dependencies into class instead of class creating them itself.
Example - without DI
Example - with DI
Here, OrderService depends on interface (IEmailSender), which makes it easier to test and swap implementations.
Advantages:
Looser coupling
Easier maintenance
Clear dependency graph
Better testability
Below is one of the best article with complete reference about Dependency Injection in C#
Understanding Dependency Injection (DI) in .NET Core with Simple Examples
Glad to help! Please mark this answer as the accepted answer if it helped you out!
Cynthia SathuragiriRank #937 · 2.2K reputationPosted Aug 19, 2026, 12:36 AM EDT
Dependency Injection (DI) in C# is a simple way to provide an object with the things it needs instead of creating them inside the class itself.
For example, without DI, we might write:
class OrderService { private EmailService emailService = new EmailService(); public void PlaceOrder() { emailService.SendEmail(); } }Here,
OrderServicecreates its ownEmailService, so both classes are tightly connected.With Dependency Injection, we can do this:
class OrderService { private readonly EmailService emailService; public OrderService(EmailService emailService) { this.emailService = emailService; } public void PlaceOrder() { emailService.SendEmail(); } }Now
EmailServiceis passed intoOrderServicefrom outside. This makes the code easier to test and maintain. If we want to replaceEmailServicewith another implementation later, we can do that without changingOrderService.In simple words, Dependency Injection means “don’t create the dependency yourself let someone else provide it to you.”
ASP.NET Core has built-in support for DI, which makes this approach very easy to use.
Carlene Lee2 reputationPosted Aug 19, 2026, 3:38 PM EDT
Dependency Injection (DI) in C# is a design pattern used to provide a class with the dependencies it needs instead of creating them inside the class.
For example, instead of a class creating its own
ILogger, the logger is passed through the constructor. This makes the code more flexible, testable, and easier to maintain.Common types of Dependency Injection are:
Constructor Injection – dependencies are provided through the constructor.
.Property Injection – dependencies are assigned through properties.
Method Injection – dependencies are passed as method parameters.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 25, 2026, 12:09 AM EDT
https://www.c-sharpcorner.com/blogs/dependency-injection-di-c-sharp
Priya PrajapatiRank #1,224 · 1.5K reputationPosted Sep 9, 2026, 1:39 AM EDT
Refer this article : https://www.c-sharpcorner.com/article/dependency-injection-in-asp-net-mvc-5
Tuhin PaulRank #7 · 71.9K reputationPosted Sep 20, 2026, 6:51 AM EDT
Dependency Injection (DI) in C# is a design pattern where a class receives the objects it depends on from outside instead of creating them itself.
For example:
public class OrderService { private readonly IEmailService _emailService; public OrderService(IEmailService emailService) { _emailService = emailService; } }Here,
OrderServicedepends onIEmailService, but it doesn't create the implementation itself. The dependency is injected through the constructor.In ASP.NET Core, we typically register it in
Program.cs:Common DI lifetimes are Transient, Scoped, and Singleton.
The main benefits are loose coupling, easier unit testing, better maintainability, and flexibility to replace implementations.