Hello Folks,
Today I was going through concepts of Interfaces in C# and came across an example on Interface. Please see the following:
Interface code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public interface IAClass
{
int AddNumber(int a, int b);
}
public interface IBClass
{
int AddNumber(int a, int b);
}
public class Class1: IAClass, IBClass
{
public int AddNumber(int a, int b)
{
return a + b;
}
}
}
Console Application Code:
using System;
using System.Collections.Generic;
using System.Text;
using ClassLibrary1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IBClass obj = new Class1();
Console.WriteLine(obj.AddNumber(3, 6));
}
}
}
I am getting the output as 9, which is correct. Now my question is that I have declared same method in both interfaces, with same signature. Class1 is implementing both Interfaces. If I am creating the object of IAClass or IBClass output is same, how does the compiler understand that which method it should call?
Why is it not throwing any error?