c# - 如何在 C# 中对变量定义中的接口(interface)求和?

标签 c# .net inheritance types interface

假设我们有 2 个接口(interface):IOneITwo,我想定义一个函数来接受同时实现这两个接口(interface)的任何类型的参数.怎么办?

最佳答案

你可以这样做:

interface IOne {void Hello();}
interface ITwo {void World();}

static void Foo<T>(T arg) where T : IOne, ITwo {
    arg.Hello();
    arg.World();
}

关于c# - 如何在 C# 中对变量定义中的接口(interface)求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22123533/

相关文章:

c# - 了解延迟执行: Is a Linq Query Re-executed Everytime its collection of anonymous objects is referred to?

c# - Linq 在 1 个循环中的 2 个数组?

c# - 如何找到引用特定 dll 的所有程序集?

c# - 绑定(bind)中的 SpecFlow 歧义

.net - MSMQ和轮询以接收消息?

python-2.7 - Python继承和__getattr__和__getattribute__

c# - 在 accord.net 的 DeepNeuralNetworkLearning 类中恢复训练

c# - 带有 IDisposable 的简单 ConsoleProgram - 没有内存减少 - 我们有泄漏?

java - 如何将用户输入添加到数组中

javascript - 深度继承链是否会减慢 V8 JavaScript 引擎中的方法查找速度?