Skip to content

Instantly share code, notes, and snippets.

View peteraritchie's full-sized avatar
😖

Peter Ritchie peteraritchie

😖
View GitHub Profile
@peteraritchie
peteraritchie / TrySingleExtensionMethod.cs
Created January 9, 2025 18:12
An extension method that uses the Try pattern to get a single value from a collection.
public static class Extensions
{
/// <summary>
/// Try to get the single value from the collection
/// </summary>
/// <typeparam name="T">The type of elements in the collection</typeparam>
/// <param name="source">the source collection</param>
/// <param name="predicate">The predicate to test elements</param>
/// <param name="item">The item found, or null</param>
/// <returns>true if found, false otherwise</returns>
@peteraritchie
peteraritchie / nullable-reference-type-tryget.md
Last active December 17, 2024 22:09
Thoughts on a method to get the value of a nullable reference type without the out value itself be nullable.

public static class X { /// Easy with structs</remarks public static bool TryGetValue(this T? nullable, ref T value) where T : struct { if (nullable.HasValue) { value = nullable.Value; return true; }

/// <summary>
/// Some extensions for <seealso cref="ImmutableList{T}"/>
/// </summary>
public static class ImmutableListExtensions
{
/// <summary>
/// Replace <paramref name="value"/> in <paramref name="source"/> based on object equality
/// </summary>
/// <remarks>
/// This method depends on the <typeparamref name="T"/> implementing Object.Equals, if it does not,

The structure of IPv4 addresses lies at the core of the internet's architecture

  • does calling out IPv4 mean many of the details in this chapter apply to IPv6?

The very structure of IPv4 addresses presents an interesting duality: they serve as both identifiers and locators.

  • "Locator" in what sense?

[Just as a street address indicates a specific location, an IP address designates a particular device on a network. The subnet mask, analogous to the zip code, guides data packets to their intended destination.](https://cwoodruff.github.io/book-network-programming-csharp/chapter02/#:~:text=just%20as%20

HTML Entities

general

HTML Description HTML Description HTML Description
&incare; care of &rx; prescription &target; target
&hybull; hypen bullet &olcir; circled white bullet &ofcir; ⦿ circled bullet
&hairsp; very thin space &ZeroWidthSpace; zero-width space

Arrows

@peteraritchie
peteraritchie / Directory.build.props
Last active February 27, 2024 20:11
A Directory.build.props file that cleans the `obj` and `bin` directories when executing `dotnet clean` or the clean operation in Visual Studio. Find latest version here: https://gist.githubusercontent.com/peteraritchie/c32008d0c20323126c908a61ef9991d6/raw/Directory.build.props
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
</PropertyGroup>
<Target Name="PostClean" AfterTargets="Clean">
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
<RemoveDir Directories="$(BaseOutputPath)" />
</Target>
</Project>
@peteraritchie
peteraritchie / visual-studio-classes.ps1
Created February 27, 2024 16:02
A work in progress for a couple (maybe more) classes to wrap some `dotnet new` commands.
<# e.g.:
. .\visual-studio-classes.ps1
$csproj = [Project]::CreateClassLib(".\Solution\ClassLib\ClassLib.csproj");
$csproj.AddClass('MyClass');
#>
class Project {
[string]$Folder;
Project() {

Visual Studio HTTP Files

env

{
  "dev": {
    "ClientSecret": {
      "provider": "AzureKeyVault",
      "secretName": "clientSecret",
@peteraritchie
peteraritchie / search-replace-try-catch.md
Created December 12, 2023 22:34
Search and replace try/catch in C#

Search for

^(?'tab'\s*)try\s*\r?\n+\k<tab>\{\r?\n(?'tried'(.*\r?\n)+)\k<tab>\}\r?\n\k<tab>catch\s+.*\r?\n\k<tab>\{\s*\r?\n(?'caught'(.*\r?\n)+)\k<tab>\}(\r?\n)+

Replace with:

${tried}