Skip to main content
gamedev.net
🔒 Locked

c# question - Generic Dictionary

Started by FreJa Apr 13, 2009 at 9:30 AM 2 replies 1.3k views
Original Post
FreJa
FreJa
Hi, At the moment I have and Interface which has this method:

Dictionary<string, T> GetAll();
Is there any way I can have a generic key? Instead of string? So the implementations of this interface can declare Dictionary GetAll(); or Dictionary GetAll(); ? Thanks!
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
FreJa
FreJa
Quote:
Original post by Telastyn
By adding a generic parameter to the owning class (or the method itself I suppose).


Right, but I already have a generic parameter (T) for the value of the dictionary, how do I add one to the key?


Quote:
It might help if we knew what you're trying to do.


I'm implementing the repository pattern. My repository interface looks like this now:

public interface IRepository<T>    {        T GetById(int id);        Dictionary<string, T> GetAll();        void Add(T entity);        void Update(T entity);        void Remove(T entity);    }


And now I want to have to implementations of this interface, one for a User where I want the GetAll() method to return a Dictionary, and one for a Contact where I want the GetAll() to return a Dictionary. So, how do I make the dictionary key generic?

Thanks again
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Telastyn
Telastyn
public interface IRepository<K,V>    {        V GetById(int id);        Dictionary<K, V> GetAll();        void Add(V entity);        void Update(V entity);        void Remove(V entity);    }


Perhaps something that is not suitable to your design, but how it could be done.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.