smwikipedia
smwikipedia

Reputation: 64205

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?

Upvotes: 1

Views: 146

Answers (1)

cadrell0
cadrell0

Reputation: 17307

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()
{
     ...
}

Upvotes: 10

Related Questions