I always run in circles with this problem. i wrote code that uses the following interface:
public interface IStorage
{
T GetElement<T>(String id) where T : class;
void SetElement<T>(String id, T element) where T : class;
}
But i am just not able to write generic get and set functions. I always end up trying to access dictionaries but all my attempts never even compile. I am mising something huge here, what is it?
The way you defined the interface, method invocation on an interface implementation requires specification of the base type
<T>. At that point, you might as well be casting.[Edited to note] Also, the base class for a generic method is scoped to the individual method, not to the class/interface, which is probably what was causing your problems.
First off, I’d simplify your interface to something like this:
Then I’d implement it something like: