using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; class Program { static void Main(string[] args) { Exception exception = null; try { new Program(); } catch (Exception ex) { Console.WriteLine(ex); exception = ex.Demystify(); } Console.WriteLine(); Console.WriteLine(exception); } static Action s_action = (string s, bool b) => s_func(s, b); static Func s_func = (string s, bool b) => (RefMethod(s), b); Action, object> _action = (Action lambda, object state) => lambda(state); static string s = ""; [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] Program() : this(() => Start()) { } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] Program(Action action) { RunAction((state) => _action((s) => action(), state), null); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static IEnumerable Iterator(int startAt) { var list = new List() { 1, 2, 3, 4 }; foreach (var item in list) { // Throws the exception list.Add(item); yield return item.ToString(); } } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static async Task MethodAsync(int value) { await Task.Delay(0); return GenericClass.GenericMethod(ref value); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static async Task MethodAsync(TValue value) { return await MethodLocalAsync(); async Task MethodLocalAsync() { return await MethodAsync(1); } } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static void RunAction(Action lambda, object state) { lambda(state); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static string RunLambda(Func lambda) { return lambda(); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static (string val, bool) Method(string value) { Func func = () => MethodAsync(value).GetAwaiter().GetResult(); var anonType = new { func }; return (RunLambda(() => anonType.func()), true); } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static ref string RefMethod(int value) { return ref s; } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static string RefMethod(in string value) { var val = value; return LocalFuncParam(value).ToString(); int LocalFuncParam(string s) { return int.Parse(LocalFuncRefReturn()); } ref string LocalFuncRefReturn() { Method(val); return ref s; } } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static string Start() { return LocalFunc2(true, false).ToString(); void LocalFunc1(long l) { Start((val: "", true)); } bool LocalFunc2(bool b1, bool b2) { LocalFunc1(1); return true; } } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static ref string RefMethod(bool value) { return ref s; } [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] static void Start((string val, bool) param) { s_action.Invoke(param.val, param.Item2); } class GenericClass { [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)] public static string GenericMethod(ref TSubType value) { var returnVal = ""; for (var i = 0; i < 10; i++) { try { returnVal += string.Join(", ", Iterator(5).Select(s => s)); } catch (Exception ex) { throw new Exception(ex.Message, ex); } } return returnVal; } } }