Is it possible to make a parameter implement two interfaces?

后端 未结 4 604
心在旅途
心在旅途 2020-12-08 20:27

Is it possible to define a function that takes in a parameter that must implement two interfaces?

(The two interfaces are ones I just remembered off the top of my he

4条回答
  •  孤城傲影
    2020-12-08 20:49

    You can:

    1) Define an interface that inherits both required interfaces:

    public interface ICombinedInterface : IComparable, ICollection {... }
    
    private void DoSomething(ICombinedInterface input) {... }
    

    2) Use generics:

    private void DoSomething(T input)
        where T : IComparable, ICollection
    {...}
    

提交回复
热议问题