// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class MyAction { public string Text { get; set; } public MyAction(string msg) { Text = msg; } } public class Something { public int ID { get; set; } public int Value { get; set; } } public class SomethingElse { public string S { get; set; } } public static class Extensions { public static Tuple If(this Tuple prev, Func f, MyAction action) { return prev.Item2 == null ? new Tuple(prev.Item1, f(prev.Item1) ? action : null) : prev; } public static Tuple If(this T o, Func f, MyAction action) { return (new Tuple(o, null)).If(f, action); } public static void DoSomethingWithMyAction(this Tuple res) { Console.WriteLine(res.Item2 == null ? "No ifs" : res.Item2.Text); } } public class Program { public static void Main(string[] args) { Something o = new Something { ID = 10, Value = 10 }; o.If(x => x.ID == 10, new MyAction("ID10")).If(x => x.Value < 20, new MyAction("Value20")).DoSomethingWithMyAction(); o.ID = 11; o.If(x => x.ID == 10, new MyAction("ID10")).If(x => x.Value < 20, new MyAction("Value20")).DoSomethingWithMyAction(); SomethingElse o2 = new SomethingElse { S = "ABC" }; o2.If(x => x.S == "A", new MyAction("AA")).DoSomethingWithMyAction(); } } }