简体   繁体   中英

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

where T : The type argument must be or implement the specified interface. 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>>
{
      ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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