Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# interfaces with same method name

I dont know if what I'd like to do is simply not possible: or I'm not thinking about it in the correct way.

I'm trying to construct a repository interface class which accepts a generic type and uses this as the basis for the return on most of its methods, ie:

public interface IRepository<T> {
    void Add(T source);
    T Find(int id);
}

This would then be inherited by an actual repository class, like so:

public class TestClientRepository : IRepository<ClientEmailAddress>, IRepository<ClientAccount> {

}

The idea is that within a ClientRepository, for example, I will want to perform operations against a few different object types (ClientAccount, ClientEmailAddress etc); but in the main the types of operations needed are all the same.

When I try to use the TestClientRepository (after implementing the Interfaces explicitly) I cannot see the multiple Find and Add methods.

Can anyone help? Thanks.

like image 823
pierre Avatar asked Sep 23 '11 14:09

pierre


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

Sure - all you've got to do is use it as the appropriate interface:

TestClientRepository repo = new TestClientRepository();

IRepository<ClientEmailAddress> addrRepo = repo;
ClientEmailAddress address = addrRepo.Find(10);

IRepository<ClientAccount> accountRepo = repo;
ClientAccount accoutn = accountRepo.Find(5);

Basically explicitly implemented interface methods can only be called on an expression of the interface type, not on the concrete type that implements the interface.

like image 62
Jon Skeet Avatar answered Sep 19 '22 14:09

Jon Skeet