Skip to content

Instantly share code, notes, and snippets.

public interface IDriver
{
void Drive();
}
Console.WriteLine("Hello world!");
using System;
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello world!");
}
}
@JasonBock
JasonBock / MaximumGenericParameterCount.cs
Last active March 13, 2022 17:01
Figuring Out How Many Generic Parameters Can Be Defined in C#
/*
I was curious to see what the maximum number of generic parameters were allowed for a method.
This was inspired by https://www.tabsoverspaces.com/233892-whats-the-maximum-number-of-arguments-for-method-in-csharp-and-in-net
To run this code, make a C# console application with the .csproj file looking like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
using BenchmarkDotNet.Attributes;
namespace ConsoleApp3
{
[MemoryDiagnoser]
public class FormatQueuedTaskMessage
{
[Benchmark]
[Arguments(3)]
[Arguments(4)]
| Method | Mean | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------- |------------:|-------:|-------:|------:|------:|----------:|
| MapUsingInline | 11.95 ns | 1.00 | 0.0153 | - | - | 64 B |
| MapUsingReflection | 2,375.37 ns | 201.25 | 0.3128 | - | - | 1320 B |
| MapUsingAutoMapper | 132.42 ns | 11.54 | 0.0248 | - | - | 104 B |
@JasonBock
JasonBock / gist:dea6707f5c2abe1997506506a2d5aca3
Last active June 18, 2019 13:02
Benchmarking different ways to negate an integer in C#
// "Inspired" by https://twitter.com/shit_sharp/status/1004360358306467844
using BenchmarkDotNet.Attributes;
using System;
namespace NegativeBenchmark
{
[MemoryDiagnoser]
public class NegatingNumbers
{
public class Foo
{
public void Bar() { }
}