Introduction
We never had a provision to define method body in C# interfaces, but C# 8.0 brings you the ability to define method body into your interface. In C# 8 on .NET Core 3.0, you can define an implementation to an interface. The main scenario is to add new members to an existing interface which is already in production.
Let's see how the old C# interface used to be before this feature.
- public class Training
- {
- public Training(string name, string cost)
- {
- Name = name;
- Cost = cost;
- }
-
- public string Name { get; set; }
-
- public string Cost { get; set; }
- }
-
- interface ITrainingService
- {
- IList<Training> Trainings { get; }
-
- void AddTraining(string name, string cost);
-
- void AddTraining(Training training);
- }
Here, we have two interface methods which you are forced to implement in your derived classes, but in C# 8.0, you can define method implementation directly inside interface which allows you to write less code during implementation and common code of different derived classes can be written directly in the interface, an alternate of abstract class.
In this example, if you want to add a training object which is going to common by adding a name, cost or by training object. If more than one class is implementing this interface, you have to write the same implementation either by creating a base abstract class or possibly in both derived classes. Now, you can use this new C# 8.0 feature and write the kind of common implementation directly in the interface.
Upgrade default interface methods
- interface ITrainingService
- {
- IList<Training> Trainings { get; }
-
- void AddTraining(string name, string cost) => AddNewTraining(name, cost);
-
- void AddTraining(Training training) => AddTraining(training.Name, training.Cost);
-
- public string GetTrainingString(Training item)
- {
- return $" Traning {item.Name} has {item.Cost}";
- }
-
- protected void AddNewTraining(string name, string cost)
- {
- Trainings.Add(new Training(name, cost));
- }
- }
- public class TrainingService : ITrainingService
- {
- public TrainingService()
- {
- TrainingData = new List<Training>();
- TrainingData.Add(new Training("c# 8.0", "$20"));
- TrainingData.Add(new Training("JS", "$40"));
- }
-
- public IList<Training> Trainings => TrainingData;
-
- private static IList<Training> TrainingData;
- }
Implementation of Interface. Here you may notice that implementation of these methods are not required to implement in the derived class.
Use of this interface in main(),
- static void Main(string[] args)
- {
- ITrainingService trainingService = new TrainingService();
- trainingService.AddTraining("time mamangement", "$20");
- trainingService.AddTraining(new Training("other", "$0"));
-
- foreach (var item in trainingService.Trainings)
- {
- Console.WriteLine(trainingService.GetTrainingString(item));
- }
-
- Console.ReadKey();
- }
Calling AddTraining() interface member implementation direclty which does not have any respective class member which execute the interface implementation.
Conclusion
Here, we learned how to upgrade our old interface safely with new member
implementation directly inside the interface.