Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
We have common method in two different interface(I1, I2), how to call method of I2 ? OR Is it possible to create same method name in different Interface, please guide me any one...

Thanks in Advance.....
Posted

Yes. All you have to do is access it via a cast to the appropriate Interface:
C#
public interface Ia
    {
    void Common();
    }
public interface Ib
    {
    void Common();
    }
public class MyClassImplicit : Ia, Ib
    {
    public void Common()
        {
        Console.WriteLine("Common");
        }
    }
public class MyClassExplicit : Ia, Ib
    {
    void Ia.Common()
        {
        Console.WriteLine("Common Ib");
        }
    void Ib.Common()
        {
        Console.WriteLine("Common Ia");
        }
    }

C#
MyClassImplicit mci = new MyClassImplicit();
mci.Common();
MyClassExplicit mce = new MyClassExplicit();
((Ia)mce).Common();
((Ib)mce).Common();
// mce.Common();  //*** Will not compile if uncommented
 
Share this answer
 
Comments
Suvabrata Roy 4-Dec-13 5:17am    
I did not Show the common one. My Vote 5
BillWoodruff 4-Dec-13 5:58am    
+5 excellent answer !

You can also invoke the explicit method using the 'as operator:

(mce as Ia).Common()

And (trivia ?) one could invoke an explicit method of an interface from another explicit interface method:

void Ia.Common()
{
Console.WriteLine("Common Ia");

(this as Ib).Common();
}

Perhaps better not to mention the last example in an interview :)
Yes we can create.
You can have a look here with this link. You will be able to understand better.
http://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name-in-c-sharp[^]
 
Share this answer
 
Yes you can.

Let me show you

C#
public interface IT1
{
 void Test();
}

public interface IT2
{
void Test();
}

public class C1 : IT1,IT2
{
void IT1.Test()
{
Console.Write("IT1 Implementation"); 
}
public void Test()
{
Console.Write("IT2 Implementation");
}
}


Enjoy...
 
Share this answer
 
C#
namespace ExplConsole
{
    class Program 
    {
        static void Main ()
        {
            System.Console.WriteLine("Permission for User1");
            User1 usr1 = new Test(); // Create instance.
            usr1.Read(); // Call method on interface.
            usr1.Write();
            System.Console.WriteLine("Permission for User2");
            User2 usr2 = new Test();
            usr2.Write();
            usr2.Execute();
            System.Console.ReadKey();
        }
    }
    interface User1
    {
        void Read();
        void Write();
    }
    interface User2
    {
        void Write();
        void Execute();
    }
    class Test : NewTest,User1, User2 
    {
        public void Read()
        {
            Console.WriteLine("Read");
        }
        public void Write()
        {
            Console.WriteLine("Write");
        }
    }
    class NewTest 
    {
        public void Execute()
        {
            Console.WriteLine("Execute");
        }
    }
}


Output:

C#
Permission for User1
   Read
   Write
   Permission for User2
   Write
   Execute
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900