This is an alternate answer that mixes contributions from JaredPar and VirtLink answers, with the following constraints:
- The switch construction behaves as a function, and receives functions as parameters to cases.
- Ensures that it is properly built, and there always exists a default function.
- It returns after first match (true for JaredPar answer, not true for VirtLink one).
Usage:
var result =
TSwitch
.On(val)
.Case((string x) => "is a string")
.Case((long x) => "is a long")
.Default(_ => "what is it?");
Code:
public class TSwitch
{
class CaseInfo
{
public Type Target { get; set; }
public Func