Skip to content

Instantly share code, notes, and snippets.

alastairs / WhitespacePatternMatching1.cs
Last active Jan 18, 2022
Using C# pattern matching for string argument checks
View WhitespacePatternMatching1.cs
// I think it would be nice to be able to use a `bool` Method Group in pattern matching statements, like
// string.IsNullOrWhitespace(). Here's an example.
public class Thing
private readonly IOptions<MyOptions> _options;
public Thing(IOptions<MyOptions> options)
_options = options switch
alastairs / EqualityDemo.cs
Created Apr 1, 2020
Equality implementation in C#
View EqualityDemo.cs
namespace EqualityDemo
public class Person : IEquatable<Person>
public string GivenName { get; }
public string FamilyName { get; }
public Address Address { get; }
public override bool Equals(object other)

Keybase proof

I hereby claim:

  • I am alastairs on github.
  • I am alastairs ( on keybase.
  • I have a public key ASA8efXB1aDLQ-vHTUfdKHZOxzIKQk1SK8EyJGJtHKxNpgo

To claim this, I am signing this object:


Letter no. 1 to Brexit Party MEPs for East of England, 2019-

Dear <name>

Congratulations on taking your seat in the eighth European Parliament. I hope you are delighted with your election and are looking forward to representing your constituents. I've been following the rise of the Brexit Party with interest over the last five months but am struggling to find detailed answers to much of your stated position on Brexit. I am sure you will want to set the highest standard of accountability and transparency in your new role -- putting the principles of Trust, Honesty and Integrity at the heart of

alastairs / Cambridge Software Crafters
Last active Apr 12, 2018
Email to the Cambridge Software Crafters re: name change and CoC
View Cambridge Software Crafters

Changes to the Cambridge Software Craftsmanship Community

Hi everyone

It's not often that we email our members, so please bear with me and read this to the end.

Recent events in the wider tech community, and reactions within the Software Craftsmanship community, have prompted wider discussions about the inclusiveness of the community, and I've decided to make some small but important changes to our local chapter.

alastairs / NCrunchBugRepro.Test.deps.json
Last active May 27, 2017
.NET Core dependencies file for an NCrunch bug repro
View NCrunchBugRepro.Test.deps.json
"runtimeTarget": {
"name": ".NETCoreApp,Version=v1.1",
"signature": "284bcae32a73d805159b5f9db2a228e815fb23c1"
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v1.1": {
"ncrunchbugrepro.test/1.0.0": {
"dependencies": {
alastairs / JsonPropertyDisplayMetadataProvider.cs
Created May 16, 2017
Implementation of `IDisplayMetadataProvider` that reads the display name from `JsonProperty` attributes.
View JsonPropertyDisplayMetadataProvider.cs
namespace Mvc.Examples
internal class JsonPropertyDisplayMetadataProvider : IDisplayMetadataProvider
public void CreateDisplayMetadata(DisplayMetadataProviderContext context)
var attributes = context.Attributes;
var jsonPropertyAttribute = attributes.OfType<JsonPropertyAttribute>().FirstOrDefault();
var displayMetadata = context.DisplayMetadata;
View IDisplayMetadataProvider.cs
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
/// <summary>
/// Provides <see cref="DisplayMetadata"/> for a <see cref="DefaultModelMetadata"/>.
/// </summary>
public interface IDisplayMetadataProvider : IMetadataDetailsProvider
alastairs / CreateRecordRequest.cs
Created May 16, 2017
Example Data Model/DTO in ASP.NET MVC
View CreateRecordRequest.cs
namespace Mvc.Examples
public class CreateRecordRequest
[Required, DisplayName("employee_id")]
public string EmployeeId { get; set; }
[Required, DisplayName("source_id")]
alastairs / ValidateModelAttribute.cs
Created May 16, 2017
A simple ASP.NET MVC FilterAttribute that validates the model in the request pipeline
View ValidateModelAttribute.cs
namespace Mvc.Examples
internal class ValidateModelAttribute : ActionFilterAttribute
public override void OnActionExecuting(ActionExecutingContext actionContext)
if (actionContext.ModelState.IsValid == false)
actionContext.Result = new BadRequestObjectResult(