I have this interface and a class implementing it:
interface Scraper
{
string DateToUrl(DateTime date);
}
class ScraperA: Scraper
{
string Scraper.DateToUrl(DateTime date)
{
return "some string based on date";
}
}
I’d like to test it. I try to add this method to ScraperA:
public void JustATest()
{
DateTime date = new DateTime(2011, 5, 31);
string url = DateToUrl(date);
Console.WriteLine(url);
}
I put that in the class definition, but the compiler complains that it can’t find DateToUrl. Why?
By prefixing the method name with
Scraperin the declaration you’re implementing the interface explicitly.This means that the method is essentially invisible unless it’s called via the
Scraperinterface.Your options:
Remove the
Scraperprefix in the declaration. The method can then be called normally:Cast your instance to the interface before calling the method: