实现与通用方法的接口 [英] Implement an Interface with Generic Methods
本文介绍了实现与通用方法的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我画一个空白的就这一个,似乎无法找到任何先前的例子,我写的。我试图实现与类的通用接口。当我实现这个接口,我认为东西是不工作的权利,因为Visual Studio中不断产生错误说我不是implmenting所有的通用接口的方法。
I'm drawing a blank on this one and can't seem to find any previous example that I wrote. I'm trying to implement a generic interface with a class. When I implement the interface I think something isn't working right because Visual Studio continually produces errors saying that I'm not implmenting all of the methods in the Generic Interface.
这儿是我的工作存根:
public interface IOurTemplate<T, U>
{
IEnumerable<T> List<T>() where T : class;
T Get<T, U>(U id)
where T : class
where U : class;
}
因此,什么应该我的课是什么样子?
So what should my class look like?
推荐答案
您应该返工你的界面,像这样:
You should rework your interface, like so:
public interface IOurTemplate<T, U>
where T : class
where U : class
{
IEnumerable<T> List();
T Get(U id);
}
然后,你可以实现它作为一个泛型类:
Then, you can implement it as a generic class:
public class OurClass<T,U> : IOurTemplate<T,U>
where T : class
where U : class
{
IEnumerable<T> List()
{
yield return default(T); // put implementation here
}
T Get(U id)
{
return default(T); // put implementation here
}
}
或者,你可以实现它具体:
Or, you can implement it concretely:
public class OurClass : IOurTemplate<string,MyClass>
{
IEnumerable<string> List()
{
yield return "Some String"; // put implementation here
}
string Get(MyClass id)
{
return id.Name; // put implementation here
}
}
这篇关于实现与通用方法的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文