Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 31-Jan-13 0:05am
Edited 31-Jan-13 0:07am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this
 

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
 
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 !!
  Permalink  
v2
Comments
mohansahu at 31-Jan-13 5:21am
   
thanks its working fine...
Mantu Singh at 31-Jan-13 5:26am
   
updated the solution!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
  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:
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.
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.150326.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100