http://www.dotnetperls.com/interface
Following program is given in the above website (1st program)
Instance is IPerl perl = new Test(); it is also giving same output with Test perl = new Test();. How can these differences be explained? Problem is highlighted.
using System;
interface IPerl
{
void Read();
}
class Test : IPerl
{
public void Read()
{
Console.WriteLine("Read");
}
}
class Program
{
static void Main()
{
IPerl perl = new Test(); // Create instance.
perl.Read(); // Call method on interface.
Console.ReadKey();
}
}
Loading
Posted Sep 21, 2013, 10:12 PM
VulpesPosted Sep 21, 2013, 7:19 PM
It's therefore permissible to:
1. Assign a reference to a Test object to a variable of type IPerl.
2. Call IPerl methods implemented by the Test class (Read in this case) via the IPerl variable.
Consequently, it doesn't make any difference whether you call the Read() method via the IPerl variable or a Test variable - the results are the same because you're calling the same method.