Possible Duplicate:
C#: Interfaces – Implicit and Explicit implementation
I was reading about Interface reimplementation.
I am not sure what does this mean?
” implicitly implementing a member and explicitly implementing a member”
Sample code:
Explicit member implementation:
public interface IUndoable { void Undo(); }
public class TextBox : IUndoable
{
void IUndoable.Undo() { Console.WriteLine ("TextBox.Undo"); }
}
public class RichTextBox : TextBox, IUndoable
{
public new void Undo() { Console.WriteLine ("RichTextBox.Undo"); }
}
Implicit member implementation:
public class TextBox : IUndoable
{
public void Undo() { Console.WriteLine ("TextBox.Undo"); }
}
Explicit interface implementation allows you to implement different interfaces which have members with same signature. Also it allows you to hide interface implementation (implemented members will be available only via reference of interface type):
In this case you have two methods
Undo()with same signature. But when you hit.on your textbox variable, none of methods will be available. You need to cast textbox variable to explicitly implemented interface type:Why would you hide some interface implementation? To keep your object API more clean. Example from .net:
List<T>implements implicitlyIList<T>, but hides implementation of non-genericIList. So, you can pass lists where non-genericIListexpected, but non-generic methods likeAdd(object value)do not pollute nice generic interface.Same with
IDisposable– you can have nice method with nameCloseand explicitly implementedDispose(which will call yourClosemethod). API will be clean – onlyClosewill be visible. But your object will be possible to use inusingblocks.