简体   繁体   English

两个具有相同名称的通用方法

[英]Two generic methods with the same name

Is not possible (except use different name) to have several generic methods with the same name but implementing different interface ? 不可能(除了使用不同的名称)有几个具有相同名称但实现不同接口的通用方法?

public IList<T> List<T>() where T : class, IMyInterface1
{

    return mylist
}

public IList<T> List<T>() where T : class, IMyInterface2
{

    return mylist
}

Thanks, 谢谢,

No, you can't overload just by generic type constaints. 不,你不能仅仅通过泛型类型的重载来重载。 You can overload by the number of type parameters, but not on their constraints. 您可以通过类型参数的数量进行重载,但不能根据其约束进行重载。

If you're implementing an interface you can use explicit interface implementation - otherwise I'd suggest just using different names. 如果您正在实现一个接口,您可以使用显式接口实现 - 否则我建议使用不同的名称。 I often find that using different names makes things clearer anyway, to be honest. 老实说,我经常发现使用不同的名字会使事情变得更清晰。

Bear in mind that the example you've given introduces natural ambiguity anyway - what would you expect to be called if the type argument implemented both interfaces? 请记住,您给出的示例无论如何都会引入自然歧义 - 如果类型参数实现了两个接口,您希望调用什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM