Implicit and Explicit implementations for Multiple Interface inheritance

Posted on Microsoft .NET Support Team See other posts from Microsoft .NET Support Team
Published on Mon, 15 Feb 2010 09:08:00 +0000 Indexed on 2010/03/16 15:31 UTC
Read the original article Hit count: 502

Filed under:
|
Following C#.NET demo explains you all the scenarios for implementation of Interface methods to classes. There are two ways you can implement a interface method to a class. 1. Implicit Implementation 2. Explicit Implementation. Please go though the sample.
using System;


 


namespace ImpExpTest


{


    class Program


    {


        static void Main(string[] args)


        {


            C o3 = new C();


            Console.WriteLine(o3.fu());


 


            I1 o1 = new C();


            Console.WriteLine(o1.fu());


 


            I2 o2 = new C();


            Console.WriteLine(o2.fu());


 


            var o4 = new C();       //var is considered as C


            Console.WriteLine(o4.fu());


 


            var o5 = (I1)new C();   //var is considered as I1


            Console.WriteLine(o5.fu());


 


            var o6 = (I2)new C();   //var is considered as I2


            Console.WriteLine(o6.fu());


 


            D o7 = new D();


            Console.WriteLine(o7.fu());


 


            I1 o8 = new D();


            Console.WriteLine(o8.fu());


 


            I2 o9 = new D();


            Console.WriteLine(o9.fu());


        }


    }


 


    interface I1


    {


        string fu();


    }


 


    interface I2


    {


        string fu();


    }


 


    class C : I1, I2


    {


        #region Imicitly Defined I1 Members


        public string fu()


        {


            return "Hello C";


        }


        #endregion Imicitly Defined I1 Members


 


        #region Explicitly Defined I1 Members


 


        string I1.fu()


        {


            return "Hello from I1";


        }


 


        #endregion Explicitly Defined I1 Members


 


        #region Explicitly Defined I2 Members


 


        string I2.fu()


        {


            return "Hello from I2";


        }


 


        #endregion Explicitly Defined I2 Members


    }


 


    class D : C


    {


        #region Imicitly Defined I1 Members


        public string fu()


        {


            return "Hello from D";


        }


        #endregion Imicitly Defined I1 Members


    }


}




Output:-



Hello C

Hello from I1


Hello from I2


Hello C


Hello from I1


Hello from I2


Hello from D


Hello from I1


Hello from I2

© Microsoft .NET Support Team or respective owner

Related posts about .NET Code Samples

Related posts about .NET