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

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