Showing posts with label Interface same method. Show all posts
Showing posts with label Interface same method. Show all posts

Monday, 17 October 2011

Two Interfaces having same method and same parameters


Code Snippet
    public interface I1
    {
        void Method();   
    }
    public interface I2
    {
        void Method();
    }
    public class Implementor : I1, I2
    {
        public void Method()
        {
            Trace.WriteLine("Method");
        }
    }
            I1 i1 = new Implementor();
            I2 i2 = new Implementor();
            i1.Method();
            i2.Method();



If you want separate methods per interface, implement them explicitly:
Code Snippet
    public interface I1
    {
        void Method();   
    }
    public interface I2
    {
        void Method();
    }
    public class Implementor : I1, I2
    {
        #region I1 Members

        void I1.Method()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region I2 Members

        void I2.Method()
        {
            throw new NotImplementedException();
        }

        #endregion
    }