是否可以使参数实现两个接口?

Pon*_*dum 23 .net c# parameters interface

是否可以定义一个接受必须实现两个接口的参数的函数?

(这两个界面是我刚想到的那个界面;不是我想要使用的界面)

private void DoSomthing(IComparable, ICollection input)
{

}
Run Code Online (Sandbox Code Playgroud)

Ken*_*art 54

您可以:

1)定义一个继承所需接口的接口:

public interface ICombinedInterface : IComparable, ICollection {... }

private void DoSomething(ICombinedInterface input) {... }
Run Code Online (Sandbox Code Playgroud)

2)使用泛型:

private void DoSomething<T>(T input)
    where T : IComparable, ICollection
{...}
Run Code Online (Sandbox Code Playgroud)

  • 1)只有在您可以更改实现两个接口的所有类以实现新接口时才有效.2)我喜欢通用路线 (9认同)
  • 在VB.Net中:私有子DoSomthing(T为{IComparable,ICollection})(ByVal输入为T)... End Sub (3认同)

Ste*_*ock 5

您可以从这两个接口继承另一个接口,并使您的参数实现该接口.

  • 仅当您可以更改实现两个接口的所有类以实现新接口时才有效. (2认同)