Interface with generic parameter vs Interface with generic methods

前端 未结 4 1152
别那么骄傲
别那么骄傲 2020-12-08 02:34

Let\'s say I have such interface and concrete implementation

public interface IMyInterface
{
    T My();
}

public class MyConcrete : IMyInterface&l         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 03:03

    Because your interface declares a generic method T My(), but you implementation does not implement a function with that specific signature.

    To achieve what you want, you need to provide the T generic parameter to the interface instead, in your first example:

    public interface IMyInterface2
    {
            T My();
    }
    
    public class MyConcrete2 : IMyInterface2
    {
        public string My()
        {
            throw new NotImplementedException();
        }
    }
    

提交回复
热议问题