Skip to content

Instantly share code, notes, and snippets.

View christiannagel's full-sized avatar
💭
working on the new book Pragmatic Microservices with C# and Azure

Christian Nagel christiannagel

💭
working on the new book Pragmatic Microservices with C# and Azure
View GitHub Profile
public class Gadget
{
public Gadget(int id, string name)
{
this.Id = id;
this.Name = name;
}
public int Id { get; }
public string Name { get; }
public class Gadget
{
public Gadget(int id, string name)
{
this.id = id;
this.name = name;
}
private readonly int id;
public int Id
@christiannagel
christiannagel / HelloWorld.cs
Created June 20, 2015 14:35
Hello, World with C# 6
using static System.Console;
class Program
{
public void Main()
{
WriteLine("Hello, World!");
}
}
@christiannagel
christiannagel / ArrayPool.cs
Last active May 26, 2017 08:41
Allocate and use a simple array
private static void UsingSimpleArrays()
{
Console.WriteLine(nameof(UsingSimpleArrays));
for (int i = 0; i < 20; i++)
{
LocalUseOfArray(i);
}
Console.WriteLine();
Console.WriteLine();
}
@christiannagel
christiannagel / ArrayPool.cs
Last active June 4, 2017 05:58
Show memory address of array
unsafe private static void ShowAddress(string name, int[] item)
{
fixed (int* addr = item)
{
Console.Write($"\t0x{(ulong)addr:X}");
}
}
@christiannagel
christiannagel / ArrayPool.cs
Created May 26, 2017 09:35
Creating arrays while using the GC
private static void UsingSimpleArraysWithGC()
{
Console.WriteLine(nameof(UsingSimpleArraysWithGC));
for (int i = 0; i < 20; i++)
{
GC.Collect(0);
LocalUseOfArray(i);
}
Console.WriteLine();
Console.WriteLine();
@christiannagel
christiannagel / ArrayPool.cs
Created May 26, 2017 09:54
Use a shared ArrayPool
private static void LocalUseOfSharedPool(int i)
{
int[] arr = ArrayPool<int>.Shared.Rent(ARRAYSIZE);
ShowAddress($"simple array {i}", arr);
FillTheArray(arr);
UseTheArray(arr);
ArrayPool<int>.Shared.Return(arr);
}
@christiannagel
christiannagel / EnumerableExtensions.cs
Created July 20, 2017 14:02
Where with error information delayed
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach (T item in source)
{
if (predicate(item))
{
yield return item;
@christiannagel
christiannagel / EnumerableExtensions.cs
Created July 20, 2017 14:16
Where with earlier error information using a separate method
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return WhereImpl(source, predicate);
}
private static IEnumerable<T> WhereImpl<T>(IEnumerable<T> source, Func<T, bool> predicate)
{
@christiannagel
christiannagel / EnumerableExtensions.cs
Last active July 22, 2017 07:39
Where with a local function
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return Iterator();
IEnumerable<T> Iterator()
{
foreach (T item in source)