using System; namespace ScreenConnect; public struct Union { private bool isFirstOrSecond; private T1 first; private T2 second; public T1 First => first; public T2 Second => second; private Union(T1 value) { isFirstOrSecond = true; first = value; second = default(T2); } private Union(T2 value) { isFirstOrSecond = false; first = default(T1); second = value; } public bool IsFirst(out T1 value) { value = first; return isFirstOrSecond; } public bool IsSecond(out T2 value) { value = second; return !isFirstOrSecond; } public static implicit operator Union(T1 t1) { return new Union(t1); } public static implicit operator Union(T2 t2) { return new Union(t2); } public override string ToString() { if (!isFirstOrSecond) { return second.SafeToString(); } return first.SafeToString(); } public TResult Match(Func t1Func, Func t2Func) { if (!isFirstOrSecond) { return t2Func(second); } return t1Func(first); } public Union MatchFirst(Func t1Func) { return Match((Func>)((T1 it) => t1Func(it)), (Func>)((T2 it) => it)); } public Union MatchSecond(Func t2Func) { return Match((Func>)((T1 it) => it), (Func>)((T2 it) => t2Func(it))); } public void Match(Proc t1Proc, Proc t2Proc) { Match(t1Proc.ProcToDefaultFunc(), t2Proc.ProcToDefaultFunc()); } }