I just came across this weird behavior today:
interface IFooBar
{
void Foo();
void Bar();
}
class FooBar : IFooBar
{
void IFooBar.Foo()
{
}
void IFooBar.Bar()
{
this.Foo();
}
}
The line this.Foo(); raises the compiler error
‘MyProject.FooBar’ does not contain a
definition for ‘Foo’ and no extension
method ‘Foo’ accepting a first
argument of type ‘MyProject.FooBar’
could be found (are you missing a
using directive or an assembly
reference?)
If I choose public methods instead of the interface.method declaration style, the code compiles:
class FooBarOk : IFooBar
{
public void Foo()
{
}
public void Bar()
{
this.Foo();
}
}
I’d like to understand why this error is raised, and how it can be worked around using the interface.method notation
To work around it, you can write:
Take a look at the Explicit Interface Implementation Tutorial for answer why
this.Foo()doesn’t work.