简体   繁体   English

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

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

I want to use following code (as example): 我想使用以下代码(例如):

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

Documentation says that using generic interface constraint is possible: 文档说可以使用通用接口约束:

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

where T : The type argument must be or implement the specified interface. 其中T:type参数必须是或实现指定的接口。 Multiple interface constraints can be specified. 可以指定多个接口约束。 The constraining interface can also be generic. 约束接口也可以是通用的。

But I really don't understand how to make such code to work, 但是我真的不明白如何使这样的代码起作用,

You must specify the second argument: 您必须指定第二个参数:

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