Friday, March 15, 2013

Interface in c#


interface IA
    {
        void Display();
    }
    interface IB
    {
        void Display();
    }
    class Model : IA, IB
    {
        void IA.Display()
        {
            Console.WriteLine("I am from A");
        }
        void IB.Display()
        {
            Console.WriteLine("I am from B");
        }
        //public void Display()
        //{
        //    Console.WriteLine("I am from the class method");
        //}

        static void Main()
        {
            Model m = new Model();
            //m.Display();
            Console.ReadLine();
        }
    }

 static void Main()
    {
        Model m = new Model();

        // Set to IA
        IA asIA = m;
        asIA.Display();

        // Or use cast inline
        ((IB)m).Display();

        Console.ReadLine();
    }

interface IA
{
   void Display();
}
interface IB
{
   void Display();
}

    public class Program:IA,IB
    {

        void IA.Display()
        {
            Console.WriteLine("I am from A");
        }

        void IB.Display()
        {
            Console.WriteLine("I am from B");
        }

        public static void Main(string[] args)
        {
           IA p1 = new Program();
           p1.Display();
           IB p2 = new Program();
           p2.Display();
        }
    }
Output will be:
I am from A
I am from B

No comments:

Post a Comment