Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have two interface with same method name and one class inheriting those interface but when i trying to create object of class and call those interface method, how i come come to know which method will execute first if i call both method..??
Posted
Updated 30-Jan-13 23:07pm
v2

Try this


C#
interface I1
   {
       void diap();
   }
   interface I2
   {
       void diap();
   }

class Imp:I1,I2
{

public void I1.diap()
        {
            throw new Exception("The method or operation is not implemented.");
        }           

       
       
        void I2.diap()
        {
            throw new Exception("The method or operation is not implemented.");
        }

}



_______________________________________________________

Now use as follows

C#
Imp f = new Imp();
            I1 i = f;  //I1 interface I1 used to select I1 behaviour use I2 for second interface
            i.diap();



best of luck !!
 
Share this answer
 
v2
Comments
mohansahu 31-Jan-13 5:21am    
thanks its working fine...
Mantu Singh 31-Jan-13 5:26am    
updated the solution!!
The answer to this question really depends on how you have implemented your interfaces in the class. In other words, have you explicitly and/or implicitly implemented the interfaces. Let's consider a simple example:
C#
public interface IFoo
{
  void SetName(string name);
  int Age { get; set; }
}

public interface IBar
{
  void SetName(string name);
  DateTime DOB { get; set; }
}

public class FooBar : IFoo, IBar
{
  public void SetName(string name)
  {
    Console.WriteLine("Name is " + name);
  }

  public int Age
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }

  void IBar.SetName(string name)
  {
    Console.WriteLine("IBar Name is " + name);
  }

  DateTime IBar.DOB
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }
}
Now, let's call this like this:
C#
IFoo foo = new FooBar();
foo.SetName("Foo");
IBar bar = new FooBar();
bar.SetName("Bar");
This would produce the output
Name is Foo
IBar Name is Foo
Now, if we don't use the interface to in the instantiation, the explicit implementation is called.
C#
FooBar fooBar = new FooBar();
fooBar.SetName("FooBar");
This produces
Name is FooBar
Okay, so you may be tempted to think that the order of the interfaces in the declaration determines which one is the explicit and which one is the implicit declaration. This is not correct. Let's change our class a bit
public class FooBar : IFoo, IBar
{
  public void SetName(string name)
  {
    Console.WriteLine("Name is " + name);
  }

  int IFoo.Age
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }

  void IFoo.SetName(string name)
  {
    Console.WriteLine("IFoo Name is " + name);
  }

  public DateTime DOB
  {
    get
    {
      throw new NotImplementedException();
    }
    set
    {
      throw new NotImplementedException();
    }
  }
}
Now, calling the same code as we did above, we see that the output has changed like this:
IFoo Name is Foo
Name is Bar
Name is FooBar
As you can see, IBar is now the explicit implementation.
 
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