# What is a Dependency

A dependency is an object a class needs to do its job. When a class creates its own dependencies with new, it's wired to them for good, and hard to test.

Apprentice · C# · OOP · Updated November 15, 2026

By Gregor Dzierzon

## After this lesson you can

- Identify a class's dependencies, and explain the problems of creating them inside the class

## Key takeaways

- A dependency is an object a class needs to do its job, such as the `Clock` that `Greeter` needs.
- A class that creates its own dependencies with `new` is hard-wired to them: to change one, you have to change the class itself.
- A `Greeter` that makes its own `new Clock()` can't be tested reliably, because its greeting depends on when the test runs.
- Dependency injection hands a class its dependencies from outside, so whoever creates the class decides which ones it gets.

Classes rarely work alone. A `ProductDao` needs a connection string. A controller needs a DAO. An order needs its menu items. When a class needs another object to do its job, that object is a **dependency**: the class *depends* on it.

## A greeter that depends on a clock

Here's a `Greeter` that says good morning or good afternoon, depending on the time. It gets the time from a `Clock`:

```csharp
Greeter greeter = new Greeter();
Console.WriteLine(greeter.Greet("Ada"));

public class Clock
{
    public virtual int Hour => DateTime.Now.Hour;
}

public class Greeter
{
    private readonly Clock clock = new Clock();

    public string Greet(string name)
    {
        return clock.Hour < 12 ? $"Good morning, {name}!" : $"Good afternoon, {name}!";
    }
}
```

`Clock` is a dependency of `Greeter`. And `Greeter` **creates its own**: `new Clock()`, right there in the class.

## The trouble with new inside a class

It works. But run it in the morning and it says *Good morning*; run it after lunch and it says *Good afternoon*. Now try to **test** it:

- A test that expects *Good morning, Ada!* passes before noon and fails after. A test whose result depends on when you run it can't be trusted.
- To test the afternoon greeting, you'd have to wait until the afternoon.
- There's no way to hand `Greeter` a different clock, because it builds its own.

The same trouble shows up everywhere a class creates what it needs:

| The class creates | So it's stuck with | And testing it means |
|---|---|---|
| `new Clock()` | The real time | Waiting for the right time of day |
| `new ProductDao("Server=localhost;Database=Northwind;...")` | That database, on that server | Having that database, with the right rows in it |
| `new EmailSender()` | Sending real email | Sending real email to someone, every test run |

A class that creates its dependencies is **hard-wired** to them: to change one, you have to change the class itself.

## The fix, in one sentence

Don't let the class create its dependencies. **Hand them in from outside**, so whoever creates the class decides which clock, which database, which email sender it gets. That's **dependency injection**, and the next lesson shows how.

## Do it: hand the greeter its hour

The greeter now has a third greeting, for the evening. But it still reads the hour from its own `new Clock()`, so nobody can check the evening greeting without waiting for the evening. Change `Greet` so the hour is handed in from outside.

`Greet` gets two values:

- `name`: the person's name, a `string` (`"Ada"`).
- `hour`: the hour of the day, an `int` from `0` to `23` (`15` is 3 p.m.).

The greeting depends on the hour:

| `hour` | `Greet("Ada", hour)` returns |
|---|---|
| 0 to 11 | `"Good morning, Ada!"` |
| 12 to 17 | `"Good afternoon, Ada!"` |
| 18 to 23 | `"Good evening, Ada!"` |

For example, at 3 p.m.:

```csharp
Greeter greeter = new Greeter();
string greeting = greeter.Greet("Ada", 15);
```

`15` is in 12 to 17, so `greeting` is `"Good afternoon, Ada!"`.

**Your job:** change the method `Greet(string name, int hour)` in the `Greeter` class so the greeting comes from the `hour` it's handed, not from the clock.

- Use `hour` everywhere the starter uses `clock.Hour`.
- Return the greeting from the table, with the person's name in it.
- Noon (`12`) is afternoon, and 6 p.m. (`18`) is evening.
- `Greeter` no longer needs its `Clock` field: take it out.

Run the starter first: which of its tests pass depends on the time of day you run it. That's the trouble this lesson is about.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

1 question to try on the lesson page.

## So what

Every `new` inside a class for something it depends on is a decision made too early. Spot those, and you've found the places where your code is hard to test and hard to change.

## Key terms

- [Dependency](https://www.ask-codey.com/glossary/dependency-oop)
- [Dependency Injection](https://www.ask-codey.com/glossary/dependency-injection)

## Cheat sheets

- [What is a Dependency](https://www.ask-codey.com/cheat-sheets/dependency-injection-what-is-a-dependency-cheat-sheet)

Source: https://www.ask-codey.com/lessons/dependency-injection-what-is-a-dependency
