Skip to content

Instantly share code, notes, and snippets.

@chrisyarbrough
Created September 14, 2023 16:23
Show Gist options
  • Select an option

  • Save chrisyarbrough/5974486c3e3df21026932785967abcb4 to your computer and use it in GitHub Desktop.

Select an option

Save chrisyarbrough/5974486c3e3df21026932785967abcb4 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Will allocate.
IList list = new List<int>();
foreach (var item in list)
{
}
// Will not allocate.
List<int> list2 = new List<int>();
foreach (var item in list2)
{
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using Microsoft.CodeAnalysis;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.DisableOptimizations)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: System.Runtime.CompilerServices.RefSafetyRules(11)]
public class Program
{
public static void Main()
{
IList list = new List<int>();
IEnumerator enumerator = list.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
object current = enumerator.Current;
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
List<int> list2 = new List<int>();
List<int>.Enumerator enumerator2 = list2.GetEnumerator();
try
{
while (enumerator2.MoveNext())
{
int current2 = enumerator2.Current;
}
}
finally
{
((IDisposable)enumerator2).Dispose();
}
}
}
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
{
"version": 1,
"target": "C#",
"mode": "Debug"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment