---
title: "What is Dependency Injection in .NET Core?"  
description: "What is Dependency Injection in .NET Core?"  
author: "Ravi Vishwakarma"  
published: 2026-03-16  
updated: 2026-04-22  
canonical: https://answers.mindstick.com/qa/116424/what-is-dependency-injection-in-dot-net-core  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# What is Dependency Injection in .NET Core?

## Answers

### Answer by Anubhav Sharma

[**Dependency Injection (DI)**](https://www.mindstick.com/interview/34477/what-is-dependency-injection-di) in .NET Core is a [design pattern](https://www.mindstick.com/forum/33927/how-to-implement-singleton-design-pattern-in-c-sharp) used to **achieve loose coupling between classes** by **providing (injecting) the dependencies a class needs from the outside**, instead of the class creating them itself.

## Simple Explanation

Instead of a class doing this:

```cs
public class Car
{
    private Engine _engine = new Engine(); // tightly coupled
}
```

With Dependency Injection, you do this:

```cs
public class Car
{
    private readonly IEngine _engine;

    public Car(IEngine engine) // dependency injected
    {
        _engine = engine;
    }
}
```

Now the `Car` class does **not depend on a specific Engine [implementation](https://answers.mindstick.com/qa/45335/who-was-the-first-minister-of-statistics-and-programme-implementation-ministry)** — it just depends on an [abstraction](https://www.mindstick.com/articles/944/abstraction-in-c-sharp) (`IEngine`).

## Why Use Dependency Injection?

- Reduces **[tight coupling](https://www.mindstick.com/forum/155753/how-we-achieve-tight-coupling-view-in-asp-dot-net-mvc)**
- Makes code **easier to test** (mock dependencies)
- Improves **maintainability**
- Promotes **[clean architecture](https://www.mindstick.com/interview/34249/what-is-mediatr-and-how-does-it-relate-to-clean-architecture)**

## Built-in DI in .NET Core

.NET Core has a **built-in IoC (Inversion of Control) container**.

You register services in `Program.cs` (or `Startup.cs` in [older versions](https://www.mindstick.com/interview/34315/is-indexeddb-supported-across-all-browsers-what-about-older-versions)):

```cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<IEngine, PetrolEngine>();
builder.Services.AddScoped<ICarService, CarService>();
builder.Services.AddSingleton<ILogger, Logger>();
```

## Service Lifetimes

.NET Core supports 3 lifetimes:

- **Transient** → New instance every time requested
- **Scoped** → One instance per request
- **Singleton** → One instance for entire application

## How It Works

- You **register services** in the container
- You **request them via constructor injection**
- The framework **automatically provides the instance**

Example:

```cs
public class CarService : ICarService
{
    private readonly IEngine _engine;

    public CarService(IEngine engine)
    {
        _engine = engine;
    }
}
```

.NET Core automatically injects `IEngine` when creating `CarService`.

## Types of Injection

- **Constructor Injection** (most common)
- Property Injection
- Method Injection

## Example Use Case

Imagine:

- `IRepository` → [Data access](https://www.mindstick.com/forum/473/what-are-the-data-access-namespaces-in-dot-net)
- `IService` → [Business logic](https://answers.mindstick.com/qa/30543/what-is-business-logic)
- `Controller` → Uses service

Each layer receives dependencies via DI, making the app modular and testable.

> ## In One Sentence
>
> *Dependency Injection in .NET Core is a built-in mechanism that automatically supplies objects (dependencies) to classes, promoting loose coupling and better code structure.*


---

Original Source: https://answers.mindstick.com/qa/116424/what-is-dependency-injection-in-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
