Interface can be implemented in 2 ways.
-
Implicit interface implementation
In implicit interface implementation, interface name will not be mentioned with the method names while implementing them
-
Explicit interface implementation
Implicit interface implementation
In implicit interface implementation, interface name will not be mentioned with the method names while implementing them. Let's consider an example:
public interface InterfaceExample
{
void Method1(int parameter1, string parameter2);
}
Now consider a class implementing the InterfaceExample interface.
{
void Method1(int parameter1, string parameter2);
}
public class ClassExample : InterfaceExample
{
public void Method1(int parameter1, string parameter2)
{
Console.WriteLine("Implementation of Method1
of InterfaceExample");
}
}
Let's see the usage of the class implementing an interface in an implicit manner.
{
public void Method1(int parameter1, string parameter2)
{
Console.WriteLine("Implementation of Method1
of InterfaceExample");
}
}
public class Program
{
static void Main(string[] args)
{
ClassExample instance = new ClassExample();
instance.Method1(2, "parameter");
Console.ReadLine();
}
}
Since the class implemented the interface implicitly, we invokde the Method1() using the variable of class type. Hence the below code is possible.
{
static void Main(string[] args)
{
ClassExample instance = new ClassExample();
instance.Method1(2, "parameter");
Console.ReadLine();
}
}
ClassExample instance = new ClassExample();
instance.Method1(2, "parameter");
instance.Method1(2, "parameter");
No comments:
Post a Comment