0

I need to use generic methods and non-generic methods in the same interface.

public interface IBRClientActions<T> where T : class { T add(T element); Hashtable add(Hashtable hashtable); void update(T element); void update(Hashtable hashtable); } 

After I implement this interface in other interface with many type

public interface IBRClientActions : IBRClientActions<AdresseClient>, IBRClientActions<Role>, IBRClientActions<LienClient> { ... } 

in my final class I need something like that

public class FinalClass : IBRClientActions { AdresseClient add(AdresseClient element) { ... } Hashtable add**<AdresseClient>**(Hashtable hashtable) { ... } .... } 

But Hashtable add<AdresseClient>(Hashtable hashtable) { ... } is not possible Actually I have ambiguous reference in methods with Hashtable.

How can I have an add method with Hashtable for each type?


Edit 1

Explicit interface implementation

Yes that is a possibility but with this solution I need to cast my class each time

Hashtable htRole = new Hashtable(); FinalClass myClass = new FinalClass(); /* not correct, ambiguity */ myClass.add(htRole); /* correct, no ambiguity but heavy writing (sorry for my bad english) */ ((IBRClientActions<Role>)myClass).add(htRole); 

1 Answer 1

3

I need to use generic methods and non-generic methods in the same interface.

I don't think that's the problem, really. I think the real problem is that you're implementing the same generic interface multiple times with different type arguments in the same class. Without that aspect, you wouldn't have a problem here.

You can use explicit interface implementation:

Hashtable IBRClientActions<Role>.add(Hashtable hashatable) { ... } 

But this becomes ugly pretty quickly. I'd try to avoid doing this if possible. Try using composition instead of inheritance.

Also note:

  • If this is System.Collections.Hashtable, you should probably be using System.Collections.Generic.Dictionary<,> if possible
  • The method name add doesn't conform to .NET naming conventions
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.