C# generic interface specialization

前端 未结 6 1739
自闭症患者
自闭症患者 2020-12-10 02:42

I wonder if it is in any way possible to specialize generic interface methods somehow in C#? I have found similar questions, but nothing exactly like this. Now I suspect tha

6条回答
  •  既然无缘
    2020-12-10 03:19

    You could do something like this:

    public interface IStorage
    {
        void Store(object data);
        void Store(T data);
    }
    
    public class Storage : IStorage
    {
        public void Store(object data)
        {
            Console.WriteLine("Generic");
        }
    
        public void Store(int data)
        {
            Console.WriteLine("Specific");
        }
    }
    

    You've typed i as IStorage and that interface doesn't define the overloaded Store method.

提交回复
热议问题