Wednesday, March 25, 2009

Interface Inheritance Naming Conflicts



Suppose you have two interfaces, lets say Interface-IA, Interface-IB & in both you have declared Name as a property as follows.

public interface IA
{

string Name { get; set; }
String Adress { get; set; }

}

public interface IB
{

string Name { get; set; }

}

Now, you write an interface -IC which derives from above 2 Interfaces as below,


public interface IC : IA, IB
{

int Id { get; set; }

}


When you are try to do the following, you will get an error saying 'Ambiguity between IA.Name and IB.Name'. 

class Test
{

    public void GetSomething()
     {
       IC objIc = new IC();
       Textbox1.Text =  objIc.Name();
     }

}

It can be solved by upcasting as below.

class Test
{

  public void GetSomething()
  {
      IC objIc = new IC();
      Textbox1.Text =  ((IA)objIc).Name();
  }

}

No comments: