Skip to content

Instantly share code, notes, and snippets.

@bitbonk
bitbonk / Program.cs
Created Apr 27, 2020
Task.WhenAll + AggregateExeption
View Program.cs
class Program
{
static async Task Main(string[] args)
{
try
{
await Task.WhenAll(
Task.Run(() =>
{
Console.WriteLine("begin one");
@bitbonk
bitbonk / recordsanddiscriminatedunions.cs
Last active Jan 24, 2020
Record and discriminated unions in C# 9
View recordsanddiscriminatedunions.cs
public class Person
{
public initonly string Firstname { get; }
public initonly string Lastname { get; }
};
enum class ByteOrBool { byte Y; bool B;}
enum class MixedType
{
@bitbonk
bitbonk / config.md
Last active Nov 11, 2019
Example Serilog Config with C# and JSON
View config.md

C# config:

var logConfig = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .Enrich.FromLogContext()
    .WriteTo.ColoredConsole(LogEventLevel.Error, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
    .WriteTo.Logger(
        c => c.WriteTo.ColoredConsole(LogEventLevel.Information, "{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3} {SourceContext}: {Message}{NewLine}")
            .Filter.ByIncludingOnly("SourceContext = 'Microsoft.Hosting.Lifetime'"))
View GenericHostWithWpfAndCaliburn.cs
public class MyCaliburnBootstrapper : BootstrapperBase
{
private IHost? host;
protected MyCaliburnBootstrapper()
{
this.Initialize();
}
private IHost BuildHost()
View 1. MainWindow.cs
private Task completion = Task.CompletedTask;
private int counter;
private async void ButtonOnClick(object sender, RoutedEventArgs e)
{
this.completion = this.completion.ContinueWith(async _ =>
{
this.counter = this.counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
View 1. MainWindow.cs
private Task completion = Task.CompletedTask;
private int counter;
private void ButtonOnClick(object sender, RoutedEventArgs e)
{
this.completion = this.completion.ContinueWith(async _ =>
{
this.counter = this.counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
View 1. MainWindow.cs
private int counter = 0;
private async void ButtonOnClick(object sender, RoutedEventArgs e)
{
counter = counter + 1;
Debug.WriteLine($"Enter ButtonOnClick {counter}");
await DoSubAsync(counter);
Debug.WriteLine($"Exit ButtonOnClick {counter}");
}
public async Task DoSubAsync(int number)
{
@bitbonk
bitbonk / Program.cs
Last active Feb 8, 2019
System.Threading.Channels (C# 8)
View Program.cs
namespace ChannelTest
{
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
class Program
{
public static async Task Main(string[] args)
@bitbonk
bitbonk / NETClassic.json
Last active Jan 21, 2019
JSON Deserailization .NET Core vs. .NET Classic
View NETClassic.json
{
"$type": "MyNamespace.Dto`6[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Double, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Single, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Uri, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.TimeSpan, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], NETClassicApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"StringProp": "my string",
"DoubleProp": 123.0,
"FloatProp": 345.0,
"ByteArrayProp": {
"$type": "System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"$value": "AQID"
},
"UriProp": "http://localhost:80",
@bitbonk
bitbonk / Program.cs
Created Jan 19, 2019
Hello World System.Threading.Channels
View Program.cs
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
class Program
{
public static async Task Main(string[] args)
{
var channel =