Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A constrained generic delegate in C#

I would like to have a delegate that is constrained to returning one of two types; an ActionResult or a string. Is this possible?

like image 805
Ben Aston Avatar asked Dec 29 '22 05:12

Ben Aston


1 Answers

No, it's not possible (at least not in C# 4 and below). There's no support for "or" constraints on generic type parameters.

There's also no constraint that restricts a type argument to be an exact type. All constraints restrict the base types of the type argument. They cannot restrict you from inheriting from them.

like image 175
mmx Avatar answered Jan 13 '23 11:01

mmx