Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET VB.NET
If a class is implementing two interfaces and both the interfaces is having a method with the same name. Then Is it possible? If yes, then how that class will implement both of those methods?
Posted 20-Aug-12 19:42pm
Edited 20-Aug-12 19:44pm
Nueman8.5K
v2
Comments
Kenneth Haugland at 21-Aug-12 0:45am
   
Overloading... You can have the same function taking different parameters, one could take char the other integers etc.
Sergey Alexandrovich Kryukov at 21-Aug-12 0:49am
   
No, you don't get the problem. I will answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think you need to have a look at the question and my answer in this link,
 
Understanding interface in C#[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Let's say, both interfaces prescribe the same method signature. Overloading won't help you, since there is no difference in name and parameters. Than you can use the fully qualified interface member names. Here are some good samples: http://stackoverflow.com/questions/7080861/c-sharp-interface-method-ambiguity[^]
It is up to you if you want to implement all in different ways, or one of them - and that will satisfy all interfacs. But in the first case, you will need to qualify the method on call.
  Permalink  
v4
Comments
Sergey Alexandrovich Kryukov at 21-Aug-12 1:02am
   
That is correct, my 5. Please see my answer where I illustrate in on example and show how to solve a more essential problem.
--SA
Zoltán Zörgő at 21-Aug-12 2:55am
   
Thank you. Oh yes, you have posted a really exhaustive answer. I like it! But as I see, deepakdynamite had a simpler problem.
Sergey Alexandrovich Kryukov at 21-Aug-12 14:35pm
   
I know, I know. Just decided to add a more tricky situation along, something that people ask often...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

This is absolutely possible. You need to use explicit interface implementation:
interface IFirst { void SomeMethod(); }
interface ISecond { void SomeMethod(); }
 
class Implementation : IFirst, ISecond {
    void IFirst.SomeMethod() { /* one implementation */ }
    void ISecond.SomeMethod() { /* another implementation */ }
} //class Implementation
 
Generally, in many cases I recommend to prefer explicit implementation over implicit one (based on a public method of the same name). The case of method name clash to be resolved demonstrates just one of those benefits.
 
Practically, this case is not so important. But there is another case where you could have clearly unambiguous syntax which is impossible to express without interfaces — two different indexed property. How to achieve the syntax below?
MyClass myInstance = //...
myInstance[3] = "first case";
myInstance["key"] = "second case";
 
How? Here is the solution:
interface IAuxIndexer { string this[int index] { get; set; } }
class MyClass {
    string IAuxIndexer.this[int index] { get { /*...*/ } set { /*...*/ } }
    public string this[string index] { get { /*...*/ } set { /*...*/ } }
}
 
This way, to have three different indexed property in one class, you need two interfaces, etc.
 
—SA
  Permalink  
v5
Comments
Zoltán Zörgő at 21-Aug-12 2:55am
   
My 5! Great!
Sergey Alexandrovich Kryukov at 21-Aug-12 3:04am
   
Thank you very much, Zoltán.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your implementing class will have one method and this will satisfy the requirements of both interfaces. It doesn't matter what type of reference variable it calls, the same method will always be executed.

Check this.
http://stackoverflow.com/questions/4638218/class-implementing-two-interfaces-which-define-the-same-method[^]
  Permalink  
Comments
Santhosh Kumar J at 21-Aug-12 0:55am
   
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

.NET does not support multiple inheritances directly because in .NET, a class cannot inherit from more than one class. .NET supports multiple inheritances through interfaces
for more...
 
Spam removed.
  Permalink  
v2
Comments
Pete O'Hanlon at 25-Feb-13 14:12pm
   
You were warned, before, about spamming the site attempting to get link backs to your site. Your account has been identified as a spammer, and will be removed. I am removing the linkbacks to your site because you are violating the spirit of Code Project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 305
2 Richard MacCutchan 270
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,834
1 Sergey Alexandrovich Kryukov 7,477
2 DamithSL 5,689
3 Maciej Los 5,329
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100