Skip to content

Instantly share code, notes, and snippets.

View richlander's full-sized avatar

Rich Lander richlander

View GitHub Profile
@richlander
richlander / Program.cs
Last active May 9, 2020 21:29
Environment information
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using static System.Console;
namespace versioninfo
{
class Program
{
@richlander
richlander / json-document.cs
Created January 29, 2019 17:21
Sample Usage of JsonDocument and JsonElement
static double ParseJson()
{
const string json = " [ { \"name\": \"John\" }, [ \"425-000-1212\", 15 ], { \"grades\": [ 90, 80, 100, 75 ] } ]";
double average = -1;
using (JsonDocument doc = JsonDocument.Parse(json))
{
JsonElement root = doc.RootElement;
JsonElement info = root[1];
@richlander
richlander / skeleton-ibufferwriter-impl.cs
Created January 29, 2019 17:13
Skeleton IBufferWriter<byte> Implementation
public class ArrayBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _written;
public ArrayBufferWriter(int initialCapacity)
{
// TODO: argument validation
_rentedBuffer = ArrayPool<byte>.Shared.Rent(initialCapacity);
@richlander
richlander / json.cs
Created January 29, 2019 17:11
Sample usage of Utf8JsonWriter
static int WriteJson(IBufferWriter<byte> output, long[] extraData)
{
var json = new Utf8JsonWriter(output, state: default);
json.WriteStartObject();
json.WriteNumber("age", 15, escape: false);
json.WriteString("date", DateTime.Now);
json.WriteString("first", "John");
json.WriteString("last", "Smith");
@richlander
richlander / SoftwareFallback-take-pointer.cs
Created January 29, 2019 17:10
Taking pointer from unmanaged constructed type
Vector256<int> SoftwareFallback(int x)
{
var result = Vector256<int>.Zero;
((int*)(&result))[0] = x;
return result;
}
@richlander
richlander / SoftwareFallback-UnSafe.cs
Last active January 29, 2019 17:11
Using Unsafe APIs
Vector256<short> SoftwareFallback(short x)
{
var result = Vector256<short>.Zero;
Unsafe.WriteUnaligned(ref Unsafe.As<Vector256<short>, byte>(ref result), value);
return result;
}
// https://github.com/dotnet/coreclr/blob/57fd77e6f8f7f2c37cc5c3b36df3ea4f302e143b/src/System.Private.CoreLib/shared/System/Runtime/Intrinsics/Vector256.cs#L1303
@richlander
richlander / SoftwareFallback-take-pointer.cs
Created January 29, 2019 17:08
Taking a pointer to an unmanaged constructed type
Vector256<double> SoftwareFallback(double x)
{
var pResult = stackalloc double[4]
{
x,
x,
x,
x,
};
Vector256<double> SoftwareFallback(double x)
{
var pResult = stackalloc double[4]
{
x,
x,
x,
x,
};
@richlander
richlander / switch-expression.cs
Created January 29, 2019 17:03
C# 8 Switch Expression Tuple Deconstruction and Positional Pattern
static State ChangeState(State current, Transition transition, bool hasKey) =>
(current, transition) switch
{
(Opened, Close) => Closed,
(Closed, Open) => Opened,
(Closed, Lock) when hasKey => Locked,
(Locked, Unlock) when hasKey => Closed,
_ => throw new InvalidOperationException($"Invalid transition")
};
@richlander
richlander / switch-expression.cs
Created January 29, 2019 17:01
C# 8 Switch Expression
static string Display(object o) => o switch
{
Point { X: 0, Y: 0 } => "origin",
Point { X: var x, Y: var y } => $"({x}, {y})",
_ => "unknown"
};