Quiz: extension methods

Quale sarà l'output?

namespace Extensions
{
  using System;
  using ExtensionMethodsDemo1;

     // Define extension methods for any type that implements IMyInterface.
     public static class Extension
     {
        public static void MethodA(this IMyInterface myInterface, int i)
        {
            Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");
        }

        public static void MethodA(this IMyInterface myInterface, string s)
        {
            Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");
        }

        public static void MethodB(this IMyInterface myInterface)
        {
            Console.WriteLine("Extension.MethodB(this IMyInterface myInterface)");
        }
    }
}
namespace ExtensionMethodsDemo1
{
    using System;
    using Extensions;

    public interface IMyInterface
    {
        void MethodB();
    }

    class A : IMyInterface
    {
        public void MethodB(){Console.WriteLine("A.MethodB()");}
    }

    class B : IMyInterface
    {
        public void MethodB() { Console.WriteLine("B.MethodB()"); }
        public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
    }

    class C : IMyInterface
    {
        public void MethodB() { Console.WriteLine("C.MethodB()"); }
        public void MethodA(object obj) { Console.WriteLine("C.MethodA(object obj)"); }
    }

    class ExtMethodDemo
    {
        static void Main(string[] args)
        {
            A a = new A();
            B b = new B();
            C c = new C();
            TestMethodBinding(a,b,c);
        }

        static void TestMethodBinding(A a, B b, C c)
        {
	    //copy from here...
            a.MethodA(1);      //write here     
            a.MethodA("hello");  //write here
            a.MethodB();    //write here         

            b.MethodA(1);        //write here    
            b.MethodB();      //write here       
            b.MethodA("hello");  //write here   

            c.MethodA(1);        //write here  
            c.MethodA("hello");  //write here 
            c.MethodB();         //write here 
            //...to here 
        }
    }
}

[Fonte: MSDN]


Technorati tags:

Print | posted @ domenica 14 febbraio 2010 12:47

Comments have been closed on this topic.