Click here to Skip to main content
15,902,938 members
Home / Discussions / C#
   

C#

 
AnswerRe: Implementing Interfaces Pin
ekolis16-Nov-11 16:43
ekolis16-Nov-11 16:43 
GeneralRe: Implementing Interfaces Pin
Richard Andrew x6416-Nov-11 16:46
professionalRichard Andrew x6416-Nov-11 16:46 
GeneralRe: Implementing Interfaces Pin
ekolis17-Nov-11 3:52
ekolis17-Nov-11 3:52 
AnswerRe: Implementing Interfaces Pin
Not Active16-Nov-11 16:45
mentorNot Active16-Nov-11 16:45 
GeneralRe: Implementing Interfaces Pin
Richard Andrew x6416-Nov-11 16:49
professionalRichard Andrew x6416-Nov-11 16:49 
GeneralRe: Implementing Interfaces Pin
Not Active16-Nov-11 16:51
mentorNot Active16-Nov-11 16:51 
GeneralRe: Implementing Interfaces Pin
Richard Andrew x6416-Nov-11 16:53
professionalRichard Andrew x6416-Nov-11 16:53 
AnswerRe: Implementing Interfaces Pin
Wayne Gaylard16-Nov-11 17:07
professionalWayne Gaylard16-Nov-11 17:07 
The main reason why you would explicitly implement a method of an interface is when you already have a method with the same signature, or you need to implement another interface with a method that has the same signature. So, say you have a method in your class Foo(string bar) and the interface requires a method with the same name and signature, you would have to explicitly implement the interface member IFoo.Foo(string bar). Another difference is in an implicit implementation, the method has to be declared as public, whereas in an explicit implementation, the method must be declared as private, meaning that the method can only be accessed by an instance of the Interface. Here is a quick demo:-

Say we have 2 interfaces IFoo and IFoo2:

C#
interface IFoo
   {
       void Foo(string bar);
   }
   interface IFoo2
   {
       void Foo(string bar);
   }



Then if we had a class that needed to implement both, one would have to be explicitly implemented:

C#
class Foobar:IFoo , IFoo2
   {
       //Explicit implementation - declared as private otherwise compiler error
       void IFoo.Foo(string bar)
       {
           MessageBox.Show("IFoo");
       }
       //Implicit implementation - declared as public otherwise compiler error
       public void Foo(string bar)
       {
           MessageBox.Show("IFoo2");
       }
   }



Now when we create an instance of class Foobar and need to call these methods we have to do so like this:

C#
Foobar f = new Foobar();
            f.Foo("x");//shows IFoo2
            //To implement IFoo.Foo we need to cast to IFoo
            IFoo y = (IFoo)f;
            y.Foo("y");//Shows IFoo


Hoep this helps
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

GeneralRe: Implementing Interfaces Pin
Richard Andrew x6416-Nov-11 17:10
professionalRichard Andrew x6416-Nov-11 17:10 
GeneralRe: Implementing Interfaces Pin
Wayne Gaylard16-Nov-11 17:17
professionalWayne Gaylard16-Nov-11 17:17 
AnswerRe: Implementing Interfaces Pin
BillWoodruff16-Nov-11 19:28
professionalBillWoodruff16-Nov-11 19:28 
AnswerRe: Implementing Interfaces Pin
BobJanova16-Nov-11 23:25
BobJanova16-Nov-11 23:25 
QuestionHow do you handle this? Pin
Rojan Gh.16-Nov-11 14:57
professionalRojan Gh.16-Nov-11 14:57 
QuestionInterop: Anyone know how to save an Excel workbook without the "Do you want to save" popup? Pin
Alaric_16-Nov-11 4:01
professionalAlaric_16-Nov-11 4:01 
AnswerRe: Interop: Anyone know how to save an Excel workbook without the "Do you want to save" popup? Pin
Not Active16-Nov-11 4:07
mentorNot Active16-Nov-11 4:07 
GeneralRe: Interop: Anyone know how to save an Excel workbook without the "Do you want to save" popup? Pin
Alaric_16-Nov-11 4:09
professionalAlaric_16-Nov-11 4:09 
AnswerRe: Interop: Anyone know how to save an Excel workbook without the "Do you want to save" popup? Pin
Alaric_16-Nov-11 4:08
professionalAlaric_16-Nov-11 4:08 
QuestionZoom window box (attached to mouse pointer) Pin
Blubbo16-Nov-11 3:39
Blubbo16-Nov-11 3:39 
QuestionWinForms - Detect End of RichTextBox Pin
Matt U.16-Nov-11 2:40
Matt U.16-Nov-11 2:40 
AnswerRe: WinForms - Detect End of RichTextBox Pin
V.16-Nov-11 3:56
professionalV.16-Nov-11 3:56 
GeneralRe: WinForms - Detect End of RichTextBox Pin
Matt U.16-Nov-11 4:48
Matt U.16-Nov-11 4:48 
GeneralRe: WinForms - Detect End of RichTextBox Pin
V.25-Nov-11 3:23
professionalV.25-Nov-11 3:23 
AnswerRe: WinForms - Detect End of RichTextBox Pin
BobJanova16-Nov-11 6:07
BobJanova16-Nov-11 6:07 
GeneralRe: WinForms - Detect End of RichTextBox Pin
Matt U.16-Nov-11 8:20
Matt U.16-Nov-11 8:20 
GeneralRe: WinForms - Detect End of RichTextBox Pin
Pete O'Hanlon16-Nov-11 8:45
mvePete O'Hanlon16-Nov-11 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.