Skip to content

Instantly share code, notes, and snippets.

@envyless
Created September 16, 2021 06:32
Show Gist options
  • Save envyless/f60ce7709e472c2fb91b3dc27728eb70 to your computer and use it in GitHub Desktop.
Save envyless/f60ce7709e472c2fb91b3dc27728eb70 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class C {
List<int> CoppyToList = new List<int>();
List<int> FooList = new List<int>();
// foreach by enumerator
public void ForeachByEnumerator()
{
foreach(var key in FooList)
{
CoppyToList.Add(key);
}
}
// .ForEach ( GC )
public void ForEachWithActionGC()
{
FooList.ForEach(_key=> CoppyToList.Add(_key));
}
// .ForEach ( None GC )
Action<int> action;
public void ForEachWithActionNonGC()
{
FooList.ForEach(action ?? (action = (key)=>{CoppyToList.Add(key);}));
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class C
{
private List<int> CoppyToList = new List<int>();
private List<int> FooList = new List<int>();
private Action<int> action;
public void ForeachByEnumerator()
{
List<int>.Enumerator enumerator = FooList.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
int current = enumerator.Current;
CoppyToList.Add(current);
}
}
finally
{
((IDisposable)enumerator).Dispose();
}
}
public void ForEachWithActionGC()
{
FooList.ForEach(new Action<int>(<ForEachWithActionGC>b__3_0));
}
public void ForEachWithActionNonGC()
{
FooList.ForEach(action ?? (action = new Action<int>(<ForEachWithActionNonGC>b__5_0)));
}
[CompilerGenerated]
private void <ForEachWithActionGC>b__3_0(int _key)
{
CoppyToList.Add(_key);
}
[CompilerGenerated]
private void <ForEachWithActionNonGC>b__5_0(int key)
{
CoppyToList.Add(key);
}
}
{
"version": 1,
"target": "C#",
"mode": "Debug"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment