c# generics error: The constraints for type parameter 'T' of method …?

后端 未结 3 2010
别跟我提以往
别跟我提以往 2021-01-02 01:41

Getting the following error:

Error 1 The constraints for type parameter \'T\' of method
\'genericstuff.Models.MyClass.GetC

相关标签:
3条回答
  • 2021-01-02 01:48

    You either need to apply the constraint to the interface method as well or remove it from the implementation.

    You are changing the interface contract by changing the constraint on the implementation - this isn't allowed.

    public interface IMyClass
    {
        int GetCount<T>(string filter) where T : class;
    }
    
    0 讨论(0)
  • 2021-01-02 01:59

    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;
    }
    
    0 讨论(0)
  • 2021-01-02 02:11

    You need to constrain your interface, too.

    public interface IMyClass
    {
        int GetCount<T>(string filter) where T : class;
    }
    
    0 讨论(0)
提交回复
热议问题