Skip to content

Instantly share code, notes, and snippets.

View mgravell's full-sized avatar
🏠
Working from home

Marc Gravell mgravell

🏠
Working from home
View GitHub Profile
protected override bool TryGetArray(out ArraySegment<T> segment)
{
if (typeof(T) == typeof(byte))
{
return MemoryMarshal.TryGetArray(Unsafe.As<Memory<byte>, Memory<T>>(ref Unsafe.AsRef(in this.memory)), out segment);
}
else
{
segment = default;
return false;
using System.Linq.Expressions;
using System.Reflection;
using System;
static class P
{
class Foo
{
public Guid Value => _value;
private readonly Guid _value;
using ProtoBuf;
using ProtoBuf.Serializers;
using System;
using System.IO;
static class P
{
static void Main()
{
const int BLOCK_COUNT = 100000;
HashSet<int> uniques = new(BLOCK_COUNT);
for (int i = 0; i < 100000; i++)
{
if (!uniques.Add(new object().GetHashCode()))
Console.WriteLine($"Collision after {uniques.Count} items");
}
// I was *expecting* to need the following; turns out that the above *by itself*
// usually produces collisions
using System;
Console.Write("Please give your age: ");
int input = Convert.ToInt32(Console.ReadLine());
if (input < 18)
{
Console.WriteLine("Not eligible for voting.");
}
else
using ProtoBuf;
using System;
static class Program
{
static void Main()
{
ReadOnlyMemory<byte> bytes = Convert.FromBase64String("CgYI3KzdiQYQidgLGgAgZiokNzk4M2Y0YjAtYjQ3My00NTY1LTkyMWUtMzI5NDk3YjAwMzQy");
var finalValue = Serializer.Deserialize<ileco.chimp.proto.FinalValue>(bytes);
Console.WriteLine(finalValue.inputGuid); // 7983f4b0-b473-4565-921e-329497b00342
using System;
using System.Reflection;
public class SomeType
{
static void Main() => new SomeType().DoTheThing();
public void DoTheThing()
{
var prop = GetType().GetProperty(nameof(AgreedInstallationDate));
var value = new DateTime(2021, 08, 17);
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
static class P
{
static Task Main() => A();
static async Task A()
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class P
{
static void Main()
{
var xml = @"<ABRPayloadSearchResults xmlns:xsd=""w3.org/2001/XMLSchema"" xmlns:xsi=""w3.org/2001/XMLSchema-instance"" xmlns=""abr.business.gov.au/ABRXMLSearch""> <response> <abnList> <numberOfRecords>2680</numberOfRecords> <abn>95437472757</abn> <abn>38023383928</abn> <abn>12839919648</abn> <abn>50165055391</abn> <abn>60306167177</abn> </abnList> </response> </ABRPayloadSearchResults>";
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
static class P
{
static void Main()
{
var xml = @"<Response><carList>