Skip to Content
Learn
Dependency Injection
Dependencies

Before we get into ASP.NET, let’s take a look at a basic dependency in C#.

In this program we have two important classes:

  • Trainer
  • LoudSpeaker

In order to work correctly, the Trainer class needs a LoudSpeaker instance. Thus LoudSpeaker will be a dependency for the Trainer class.

Instructions

1.

In Trainer.cs, in the Trainer constructor, instantiate a LoudSpeaker object and store it in the speaker field.

You instantiate LoudSpeaker with the parameterless constructor:

new LoudSpeaker()
2.

In the body of Trainer.BeginTraining(), use the speaker by adding this line:

speaker.Amplify("Time to sweat");
Folder Icon

Sign up to start coding

Already have an account?