C# and .NET interview question – Demonstrate implementation of interface with same method name in C#

This is one of the typical .Net interview questions and is also the favorable question of interviewers.

Let’s first try to understand what exactly the question means.

Suppose we have two interfaces namely I1, I2 and both the interface have a single method with same name like below code snippet.

interface I1
{
void Method1();
}
interface I2
{
void Method1();
}

In above code snippet you can see that we have two interfaces with same method name.

Now, let’s create a class which inherits both interfaces I1, I2 and also do the implementation like below code snippet.

class A : I1, I2
{
public void Method1()
{
Console.WriteLine("Who has Called me......");
}
}

In above code snippet you can see that I have implemented method only once.

Now let’s see if this compile or not.

You can see that the compile successfully, this means that we have not break any of the rule.

Now, let invoke both the interfaces method in main class like below code snippet.

static void Main(string[] args)
{
I1 obj1 = new A();
obj1.Method1();
I2 obj2 = new A();
obj2.Method1();
Console.ReadLine();
}

Now, just run the application and you will see result like below diagram.

The above code works fine but our concern is to have two different for both the interfaces. So in order to achieve this we have to “ExplicitInterface” method implement.

Now, let’s see how exactly we can achieve this practically.

Step1: – create a new console application for that Go To> New > File > Project > Windows > Select Console Application.

Step2: – create two interfaces like below code snippet.

interface I1
{
void Method1();
}
interface I2
{
void Method1();
}

Step3: – create a class which inherits both the interfaces and
implement both the interfaces methods like below code snippet.

class A : I1,I2
{
void I1.Method1()
{
Console.WriteLine("I have been called by I1.......");
}

void I2.Method1()
{
Console.WriteLine("I have been called by I2.......");
}
}

In above code snippet you can see that I have used “ExplicitInterface” method implementation.

void I1.Method1()// here I1.Method1() will indicate the Interface I1.
void I2.Method1()// here I2.Method1() will indicate the Interface I2.

Step4: – now just invoke both the interfaces methods in the main class like below code snippet.

static void Main(string[] args)
{
I1 obj1 = new A();
obj1.Method1();
I2 obj2 = new A();
obj2.Method1();
Console.ReadLine();
}

Once you have done with all the above steps now just run your application and you will see the result like below diagram.

Now from the above result you can see that both Interfaces methods are having different implementations.

View the following video how can we implement interfaces with same method names in C#: –


Also see our tutorials on important .NET interview questions

Regards,

View more author’s blog on Most asked .NET interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s