简体   繁体   中英

How to add 2 new() constraint for to type parameters?

I want something like this:

public static TTo JumpTo<TFrom, TTo>(this TFrom from_page) 
       where TTo : new() TFrom : new()
{
    ...
}

And I want to enforce that TFrom and TTo are both derived from a base type.

And I want to make this method as an extension method of TFrom type.

Is it possible ? And what's the correct syntax?

Put the keyword where before each type.

public static TTo JumpTo<TFrom, TTo>(this TFrom from_page) 
    where TTo : SomeBaseType, new() 
    where TFrom : SomeOtherBaseType, new()
{
     ...
}

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