Saturday, September 13, 2014

Interface Magic (at least to me)

Didn't know that interface could intercept the method of the base class even that base class is not implementing the interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
                     
public class Program
{
    public static void Main()
    {
        IRobot r = new Robot();
 
        r.SaySomething();
    }
}
 
 
public class Machine
{
    public void SaySomething()
    {
        Console.WriteLine("Hello");
    }
}
 
public interface IRobot
{
    void SaySomething();
}
 
 
public class Robot : Machine, IRobot
{
// and we don't have to implement IRobot here   
}


Live Code: https://dotnetfiddle.net/cqdt8z


Output:
Hello

I still have to find a use for that code found somewhere



Happy Coding!

No comments:

Post a Comment