Skip to content

Instantly share code, notes, and snippets.

View aalmada's full-sized avatar
🏠
Working from home

Antão Almada aalmada

🏠
Working from home
View GitHub Profile
public static int Count<TEnumerable, TEnumerator, TSource>(this TEnumerable source)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
{
var counter = 0;
using var enumerator = source.GetEnumerator();
checked
{
while (enumerator.MoveNext())
counter++;
public class Enumerable<T>
{
public Enumerable<T> GetEnumerator()
=> this;
public T Current
=> default;
public bool MoveNext()
=> default;
@aalmada
aalmada / Count.IValueEnumerable.cs
Last active April 25, 2021 22:21
Sample for the NetFabric.Hyperlinq Post.Mortem article
public static int Count<TEnumerable, TEnumerator, TSource>(this TEnumerable source)
where TEnumerable : IValueEnumerable<TSource, TEnumerator>
where TEnumerator : struct, IEnumerator<TSource>
{
using var enumerator = source.GetEnumerator();
var count = 0;
while (enumerator.MoveNext())
count++;
return count;
}
namespace NetFabric.Hyperlinq
{
public interface IValueEnumerable<out T, out TEnumerator>
: IEnumerable<T>
where TEnumerator : struct, IEnumerator<T>
{
new TEnumerator GetEnumerator();
}
public interface IValueReadOnlyCollection<out T, out TEnumerator>
@aalmada
aalmada / UsingBlockExpression.cs
Last active March 19, 2021 18:31
The C# 'using' block defined by an expression tree
public static Expression Using(ParameterExpression variable, Expression content)
{
return variable.Type switch
{
{IsValueType: true} => ValueTypeUsing(variable, content),
_ => ReferenceTypeUsing(variable, content)
};
static Expression ValueTypeUsing(Expression variable, Expression content)
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;
namespace ConsoleApp2
{
[MemoryDiagnoser]
[DisassemblyDiagnoser(printSource: true)]
public class ArrayBenchmarks
{
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using System.Linq;
namespace NetFabric.Hyperlinq.Benchmarks
{
[SimpleJob(RuntimeMoniker.Net472, baseline: true)]
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
[MemoryDiagnoser]
public class RangeContainsBenchmarks
public class RefEnumerationVariableAnalyzerTests : CodeFixVerifier
{
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
=> new RefEnumerationVariableAnalyzer();
protected override CodeFixProvider GetCSharpCodeFixProvider()
=> new RefEnumerationVariableCodeFixProvider();
[Theory]
[InlineData("TestData/HLQ004/NoDiagnostic/NoRef.cs")]
<ItemGroup>
<Compile Remove="TestData\**\*.Fix.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="TestData\**\*.Fix.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>