Here is my code
public interface ITranslator<E, R>
{
E ToEntity<T>(R record);
}
class Gens : ITranslator<string, int>
{
#region ITranslator<string,int> Members
public string ToEntity<MyOtherClass>(int record)
{
return record.ToString();
}
#endregion
}
When I compile this, I get an error Type parameter declaration must be an identifier not a type
Why is that I cannot have ToEntity<MyOtherClass> but can only have ToEntity<T> ??
Edit: what is MyOtherClass doing ? I am converting between entities(POCOs equivalent of Entity framework) and record(Object returned by the framework) for multiple tables/classes. So I would want to use this to do my class specific conversion
Your interface has a generic
ToEntity<T>method that you’ve made non-generic in your implementation classGensasToEntity<MyOtherClass>. (A generic method could take any type parameter, possibly given certain constraints onT. YourGensclass is trying to provide a definition forToEntityonly for the type parameterMyOtherClass, which defeats the purpose of generics.)In your code example, it’s unclear how your
Gensclass is trying to use theMyOtherClasstype; it’s certainly not involved in the logic ofToEntity. We’d need more information to be able to guide you further.To illustrate, here’s what your current definition of the
ITranslator<E, R>interface offers, in plain English:Your
Gensclass, on the other hand, the way it’s currently designed, “implements” the above interface like so:From these two descriptions, it’s clear that the
Gensclass is not really doing what theITranslator<E, R>interface guarantees. Namely, it is not willing to accept a user-specified type for itsToEntitymethod. That’s why this code won’t compile for you.