My Photo

Shafqat's Recent Online Activity

« Back in Dhaka, New Job | Main | Timesaver: Awesome New C# Property Syntax Shortcut »

February 24, 2008

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54ee5d547883300e5506d87218833

Listed below are links to weblogs that reference .NET Basics : Explicit vs Implicit Interface Implementation:

Comments

Kumana

Good and simple explanation. Thanks!

Moim Hossain

Last night, I was thinking about explicit and implicit interface implementation stuffs [ I am not married so, usually I don’t find anything important to think at night :P ] , and I have discovered a good example of explicit implementation of any interface- and I decided to put my thoughts here- if somebody find it interesting and useful.
Well, we can consider a simple scenario to get a good idea about the usage. Let consider, you have the following interfaces (I am using the Microsoft implementations as an example, so I think I am safe enough from controversy).

interface IDbConnection
{
String ConnectionString {get; set;}
}
interface IDbCommand
{
IDbConnection Connection { get; set;}
}

Now, if you implement these interfaces by SqlConnection and SqlCommand classes- you will find that your SqlCommand class returning a Connection object of type IDbConnection (though the underlying connection object is a SqlConnection instance). This is not a problem but does not seem to be a good one either. Cause all the clients using SqlCommand need to cast the connection instance to a SqlConnection before using any Sql specific tasks.
Did you notice that the .net base class libraries resolved this problem with a beautiful approach- and that is nothing but using the explicit interface implementation feature.
Here is what they did..

abstract class DbConnection : IDbConnection
{
Public String ConnectionString { get; set;}
}
Abstract class DbCommand : IDbCommand
{
IDbConnection IDbCommand.Connection { get; set; }
public DbConnection Connection { get; set; }
}
Now the Sql Implementation goes like..
public sealed class SqlCommand : DbCommand
{
public SqlConnection Connection { get; set; }
}
See, how nice this implementation is. The Sql specific class SqlCommand now has a SqlConnection instance rather having an IDbConnection. And if you cast the SqlCommand to an IDbCommand than you will get the IDbConnection- fair logical – right?
To me, this is the best usage of explicit interface implementation- cause; there was a time when I was quite unhappy with the problem that a specific class was bound to return an abstract instance. And .NET explicit interface implementation resolved that for me. I love .NET :)

Jeff

Did you mean:

"This is a problem since we want to have 2 different code to start the engine. So we would have to go to *explicit* implementation and write code like this"

instead of:

"This is a problem since we want to have 2 different code to start the engine. So we would have to go to implicit implementation and write code like this"

Since you would need to use explicit implementation to resolve the issue of duplicate method names.

The comments to this entry are closed.

Feeds

Pages