Skip to content

Instantly share code, notes, and snippets.

View benaadams's full-sized avatar
🦆
rubber duck debugging

Ben Adams benaadams

🦆
rubber duck debugging
View GitHub Profile
@benaadams
benaadams / Client.cs
Last active January 27, 2022 23:42
Testing ValueTask pooling
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
class Program
{
const string api = "http://127.0.0.1:5001/";
using System;
using System.Threading;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Runtime.CompilerServices;
public class Abstract
{
@benaadams
benaadams / modernizing-csharp9.md
Last active November 11, 2020 11:50 — forked from richlander/modernizing-csharp9.md
Modernizing a codebase for C# 9

Modernizing a codebase for C# 9

There are lots of cases that you can improve. The examples use nullable reference types, but only the WhenNotNull example requires it.

Use the property pattern to replace IsNullorEmpty

Consider adopting the new property pattern, wherever you use IsNullOrEmpty.

string? hello = "hello world";
public static int Entropy(this string s)
{
if (string.IsNullOrEmpty(s)) return 0;
var array = ArrayPool<char>.Shared.Rent(s.Length);
s.CopyTo(0, array, 0, s.Length);
var span = array.AsSpan(0, s.Length);
span.Sort();
@benaadams
benaadams / Microservice.cs
Last active May 20, 2020 13:11
Microservice Lib + App
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace MicroserviceLib
{
public class Microservice : Controller, IStartup
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using nint = System.Int64;
public class SequenceEqualThreshold
{
static void Main(string[] args)
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
namespace StackTests
{
public class TestStackSizes
{
const int Iters = 128;
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
using nint = System.Int64;
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using static Microsoft.AspNetCore.Builder.ArgumentType;
namespace Microsoft.AspNetCore.Builder
{
public static class RequestDelegateExtensions
public static class TaskExtensions
{
public static async Task<T[]> WhenAllIgnoreErrors<T>(this Task<T>[] tasks, TimeSpan timeout)
{
try
{
await Task.WhenAll(tasks).TimeoutAfter(timeout).ConfigureAwait(false);
}
catch { /* ignored */ }