Explicit interface implementation in .NET Part 3

In the previous post we started looking into how explicit interface implementation is done in code. Visual Studio makes it a breeze and you can easily produce code like we saw previously:

int IMultipliable.Calculate(int a, int b)
{
	...
}

So now we can reach the Calculate methods as follows:

Calculator calc = new Calculator();
int calcCalculate = calc.Calculate(2, 5);

ISummable summable = new Calculator();
int calcSummable = summable.Calculate(2, 5);

IMultipliable multiplier = new Calculator();
int calcMultiplier = multiplier.Calculate(2, 5);

“calcCalculate” will be 7 as we call the Calculate method of the Calculator object. It was implicitly implemented from the ISummable interface.

“calcSummable” will also be 7 of course, as it calls the same implementation as above.

“calcMultiplier” will be 10 as it calls the explicitly implemented Calculate method of the IMultipliable interface.

There’s nothing stopping us from implementing both interfaces explicitly and remove the implicit Calculate implementation:

public class Calculator : ISummable, IMultipliable
{		
	int IMultipliable.Calculate(int a, int b)
	{
		return a * b;
	}

	int ISummable.Calculate(int a, int b)
	{
		return a + b;
	}
}

If we do that then the following code will cause the compiler to complain:

Calculator calc = new Calculator();
int calcCalculate = calc.Calculate(2, 5);

There’s no Calculate method defined for the Calculator object any more.

This solves the mystery behind the Add method of ConcurrentDictionary of the first part of this mini-series:

ConcurrentDictionary<string, string> concurrentDictionary = new ConcurrentDictionary<string, string>();
concurrentDictionary.Add("Key", "Value");

The Add method of the IDictionary interface is explicitly implemented by the ConcurrentDictionary object. That’s why the Add method is not available directly on ConcurrentDictionary whereas it is available if we declare ConcurrentDictionary as an interface type:

IDictionary<string, string> concurrentDictionary = new ConcurrentDictionary<string, string>();
concurrentDictionary.Add("Key", "Value");

We still haven’t discussed why we’d ever choose to implement an interface explicitly. We’ll look into that in the next post which finishes this short series.

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

3 Responses to Explicit interface implementation in .NET Part 3

  1. darshana3g says:

    Awesome!,followed all three posts really good explanation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: