Skip to main content

What is dependency injection in C#?

Junior C#
Quick Answer DI means giving a class its dependencies from outside rather than letting it create them itself. Instead of new SqlRepository() inside a service, you receive an IRepository through the constructor. Makes classes easy to test (swap real for mock) and keeps them loosely coupled. ASP.NET Core has DI built in.

Answer

Dependency Injection (DI) provides required objects from outside rather than creating them internally.

It improves modularity, testability, and maintainability.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice