You can implement interface implicitly and explicitly. The difference between both approaches is that if you implement it implicitly then you are bound to call methods via interface only.
There are two ways to implement interface into your class - Implicitly and Explicitly. And the main difference is that for implicit implementation you become bound to call method via interface only. Let see this by an example.
public interface ITest
{
void Print();
}
public class Test1 : ITest
{
public void Print()
{
Console.WriteLine("Test1.Print() called.");
Console.WriteLine("This is an example of explicit implementation of interface "
+ "and method can be called via interface as well as concrete classs.");
}
}
public class Test2 : ITest
{
void ITest.Print()
{
Console.WriteLine("Test2.Print() called.");
Console.WriteLine("This is an example of implicit implementation of interface "
+ "and method can only be called via interface.");
}
}
You can notice in above example that in class Test2, we have implemented interface method Print() with its name like void ITest.Print() and also we have not added amy access modifier to this. This is an example of iimplicit implementation. On the other hand if you see implementation of same method in class Test1, we have done this as public void Print(). This is explicit implementation. Now let see what is the difference between two.
class Program
{
static void Main(string[] args)
{
ITest testInterface = new Test1();
testInterface.Print();
Test1 test1Class = new Test1();
test1Class.Print();
testInterface = new Test2();
testInterface.Print();
}
}
Till above point it will work fine and here is the output.
But if try to write below code, it will throw error stating that Test2 does not contains definition of Print() and no accessible extension method Print() accepting first argument of type Test2 could be found
Test2 test2Class = new Test2();
test2Class.Print();
Hope this helps.