I am wondering why I cant get such simple thing like this on google. This code is not compilable. How can I do this?
public class TestStep<StartEvent, CompletedEvent> where StartEvent : MyBase1, MyInterface1, new() && where CompletedEvent : MyBase2, MyInterface2, new() { }
Please help.
There can be more than one constraint associated with a type parameter. When this is the case, use a comma-separated list of constraints. In this list, the first constraint must be class or struct or the base class.
The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.
Declaring those constraints means you can use the operations and method calls of the constraining type. If your generic class or method uses any operation on the generic members beyond simple assignment or calling any methods not supported by System. Object, you'll apply constraints to the type parameter.
You can specify one or more constraints on the generic type using the where clause after the generic type name. The following example demonstrates a generic class with a constraint to reference types when instantiating the generic class.
Try without the "&&"
public class TestStep<StartEvent, CompletedEvent> where StartEvent : MyBase1, MyInterface1, new() where CompletedEvent : MyBase2, MyInterface2, new() { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With