繁体   English   中英

如何使用通用接口作为通用约束?

[英]How to use generic interface as generic constraint?

我想使用以下代码(例如):

public static IEnumerable<SomeGenericType> append_list<T>(T a, T b) where T : IEnumerable<SomeGenericType>
{
    return a.Concat(b);
}

文档说可以使用通用接口约束:

https://msdn.microsoft.com/zh-CN/library/d5x73970.aspx

其中T:type参数必须是或实现指定的接口。 可以指定多个接口约束。 约束接口也可以是通用的。

但是我真的不明白如何使这样的代码起作用,

您必须指定第二个参数:

public static IEnumerable<SomeGenericType<M>> append_list<T, M>(T a, T b) 
     where T : IEnumerable<SomeGenericType<M>>
{
      ...
}

暂无
暂无

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

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