0

Below I will describe my problem with a short sample code:

// model interface and implmentation
public interface IModel { }

public class Model : IModel{}

// generic interface which constraint T from IModel
public interface IBaseInterface<T> where T:IModel{ }

public interface IDerivedInterface : IBaseInterface<Model>{}


public class Test
{
    void FuncTest<T>() where T:IBaseInterface<IModel>
    {
        
    }

    void CallFunc()
    {
        // error: ... see below
        FuncTest<IDerivedInterface>();
    }
}

error info is :The type 'IDerivedClass' must be convertible to 'IBaseClass<IDerivedClass>' in order to use it as parameter 'T' in the generic method 'void Test.FuncTest<T>()'

my understanding is: Model implements the IModel interface, and IDerivedInterface implements the IBaseInterface interface,then IDerivedInterface should be able to be converted to IBaseInterface<IModel> type,but compiler tells me it cannot convert.

I have read related questions on stackoverflow such as:

  1. The type must be convertible in order to use it as parameter in the generic class
  2. error CS0309: The type ... must be convertible to ... in order to use it as parameter in the generic type or method
  3. The type 'TNestedInterface' must be convertible to 'INestedInterfaceTest' in order to use it as parameter 'TNestedInterface'
  4. Can not use parameter in generic class, because is not convertible to base class, although all constraints seem to be good
  5. The type 'T' must be convertible in order to use it as parameter 'T' in the generic type or method

I guess the reason may be related to covariance and inversion, ut the answer to the question in the link above doesn't make me understand the problem, can anyone give me a clear explanation?

enter image description here error info screenshot

  1. ide: rider 2022.2 version
  2. .Net version: .Net 6
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • My god....you created a method called `Func`. I thought you were using [Func](https://learn.microsoft.com/en-us/dotnet/api/system.func-1?view=net-7.0). That's confusing. I suggest you choose a different name. – ProgrammingLlama Oct 11 '22 at 01:17
  • @ProgrammingLlama Still the same error after I changed the function name to `FuncTest`,error screenshot I added in my answer below – zhenghao li Oct 11 '22 at 01:19
  • Why did you add an answer if it doesn't answer your question? And I didn't say it would resolve your question. I said it would make your question less confusing. – ProgrammingLlama Oct 11 '22 at 01:19
  • 1
    Does [this](https://stackoverflow.com/a/4653199/3181933) answer your question? Or perhaps [this](https://stackoverflow.com/a/4038168/3181933) or [this](https://stackoverflow.com/a/1817341/3181933)? I'm sure how `IList` in these questions is basically identical to `IBaseInterface` in your question. – ProgrammingLlama Oct 11 '22 at 01:20
  • 1
    @ProgrammingLlama thx, i think I've figured out why, the solution is modified this line `public interface IBaseInterface where T:IModel{ }` (add `out` symbol) – zhenghao li Oct 11 '22 at 01:41

0 Answers0