Using explicit functions from your Interface in C#

I was just wondering what the difference was in using implicit or explicit functions when implementing an interface in C#

For example we have a CarService class which inherits from an interface….

Class CarService : ICarService

Than, an implicit methodheader in the CarService class might look like:

Public List<Car> GetCars(CarType carType)

while an explicit methodheader looks like:

List<Car> ICarService.GetCars(CarType carType)

Now the difference comes up when you actually want to use the CarService class and call that method.

Suppose you do:

CarService myCarService = new CarService();

This way, the explicit methods, which define the methods to be part of the interface will than NOT be available!

So if you try:

myCarService.GetCars(.... <--- Method not accessible!!!!

To access the methods that have been made explicit with the interface-precies, create the CarSErvice class using the Interface header.
To create the myCarService like so:

ICarService carRep = new CarService();

The above line is completely valid, because CarService is implementing the ICarService interface.

Than you will be able to use the explicit interface method:

carRep.GetCars --> is now available...

Using interfaces for classes is talking about setting up 'contracts' where classes should adhere to. When possible, try building the interface-methods explicitly, so that you are making use of these accessibility stuff C# had made for you. Also, this means you are 'talking to the interface' instead of just talking to the CarService class.

More documentation can be found on the microsoft site about explicit interfaces in c#.