How can we prevent the conflicts in methods with same names, signatures belonging to multiple interfaces when these interfaces are implemented?

 Posted by Ddd on 2/6/2011 | Category: C# Interview questions | Views: 2722 | Points: 40
Answer:

Consider the following code snippet:

Solution:
1)Put the interface name before the method name in the implementation class.
2)remove the modifier public before the method name in the implementation class.

using System;

namespace zz
{
interface i1
{
int add(int a);
}
interface i2
{
int add(int b);
}
class abc:i1,i2
{
int i1.add(int a)
{
return a*5;
}
int i2.add(int b)
{
return b*10;
}
static void Main()
{
i1 p=new abc();
Console.WriteLine(p.add(5));
i2 q=new abc();
Console.WriteLine(q.add(5));
}
}
}


//output:
25
50


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Naimishforu on: 2/7/2011 | Points: 10
Check out this article for more details :

http://www.dotnetfunda.com/articles/article133.aspx

Login to post response