Getting the following error:
Error 1 The constraints for type parameter '
T
' of method
'genericstuff.Models.MyClass.GetCount<T>(string)
' must match the constraints for type
parameter 'T
' of interface method 'genericstuff.IMyClass.GetCount<T>(string)
'. Consider
using an explicit interface implementation instead.
Class:
public class MyClass : IMyClass
{
public int GetCount<T>(string filter)
where T : class
{
NorthwindEntities db = new NorthwindEntities();
return db.CreateObjectSet<T>().Where(filter).Count();
}
}
Interface:
public interface IMyClass
{
int GetCount<T>(string filter);
}
You are restricting your T generic parameter to class in your implementation. You don't have this constraint on your interface.
You need to remove it from your class or add it to your interface to let the code compile:
Since you are calling the method CreateObjectSet<T>()
, which requires the class constraint, you need to add it to your interface.
public interface IMyClass
{
int GetCount<T>(string filter) where T : class;
}