Skip to content

Instantly share code, notes, and snippets.

Avatar

Rich Lander richlander

View GitHub Profile
@richlander
richlander / Program.cs
Last active May 9, 2020
Environment information
View Program.cs
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 Jan 29, 2019
Sample Usage of JsonDocument and JsonElement
View json-document.cs
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 Jan 29, 2019
Skeleton IBufferWriter<byte> Implementation
View skeleton-ibufferwriter-impl.cs
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 Jan 29, 2019
Sample usage of Utf8JsonWriter
View json.cs
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 Jan 29, 2019
Taking pointer from unmanaged constructed type
View SoftwareFallback-take-pointer.cs
Vector256<int> SoftwareFallback(int x)
{
var result = Vector256<int>.Zero;
((int*)(&result))[0] = x;
return result;
}
View SoftwareFallback-UnSafe.cs
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 Jan 29, 2019
Taking a pointer to an unmanaged constructed type
View SoftwareFallback-take-pointer.cs
Vector256<double> SoftwareFallback(double x)
{
var pResult = stackalloc double[4]
{
x,
x,
x,
x,
};
View SoftwareFallback-UnSafe.cs
Vector256<double> SoftwareFallback(double x)
{
var pResult = stackalloc double[4]
{
x,
x,
x,
x,
};
@richlander
richlander / switch-expression.cs
Created Jan 29, 2019
C# 8 Switch Expression Tuple Deconstruction and Positional Pattern
View switch-expression.cs
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")
};
View switch-expression.cs
static string Display(object o) => o switch
{
Point { X: 0, Y: 0 } => "origin",
Point { X: var x, Y: var y } => $"({x}, {y})",
_ => "unknown"
};