Home ASP.NETC# and .NET interview question - How will you implement interface with same method name in C#?
C# and .NET interview question - How will you implement 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 .NET interview questions
Regards,
View more author’s blog on.NET interview questions




Write a Comment
All fields marked with * are mandatory
ASP.NET interview questions: - Can you explain Method of Sorting GridViewcontrol in ASP.NET?
Sorting allow you to sort the GridViewcontrol data in Ascending or Descending order.... Read More
By : Shiv Prasad Koirala | Aug 4th, 2011 | ASP.NET
WCF Interview questions:- Which binding do we need to use for WCF REST?
In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More
By : Shiv Prasad Koirala | Nov 16th, 2011 | WCF
.NET interview questions: - Can you elaborate project life cycle?
In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jan 17th, 2012 | .Net
.NET interview questions: - How will you distinguish between ForeGround and BackGround Threading?
threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time.... Read More
By : Shiv Prasad Koirala | Sep 27th, 2011 | ASP.NET
C# interview questions: - Explain anonymous methods in .NET?
n simple words Anonymous Methods means method which are coded inline or methods without method name.... Read More
By : Shiv Prasad Koirala | Aug 5th, 2011 | C#
ASP.NET interview questions: - Can you explain Method of Sorting GridViewcontrol in ASP.NET?
Sorting allow you to sort the GridViewcontrol data in Ascending or Descending order.... Read More
By : Shiv Prasad Koirala | Aug 4th, 2011 | ASP.NET
WCF Interview questions:- Which binding do we need to use for WCF REST?
In this article we will show Binding used for WCF REST. For more articles and videos visit us on www.questpond.com... Read More
By : Shiv Prasad Koirala | Nov 16th, 2011 | WCF
.NET interview questions: - Can you elaborate project life cycle?
In this article we will explain about project life cycle. For more articles and videos visit us on http://www.questpond.com/... Read More
By : Shiv Prasad Koirala | Jan 17th, 2012 | .Net
.NET interview questions: - How will you distinguish between ForeGround and BackGround Threading?
threading is a parallel processing unit and helps you to access multiple tasks at a one moment of time.... Read More
By : Shiv Prasad Koirala | Sep 27th, 2011 | ASP.NET
C# interview questions: - Explain anonymous methods in .NET?
n simple words Anonymous Methods means method which are coded inline or methods without method name.... Read More
By : Shiv Prasad Koirala | Aug 5th, 2011 | C#
Article Categories
YouTube Videos