Skip to content

Instantly share code, notes, and snippets.

public static class LocationExtensions
{
private const double RadianHalfPiToDegrees = 90.0;
private const double RadianPiToDegrees = 180.0;
private const double RadianTwoPiToDegrees = 360.0;
private static readonly Location _center = new Location(latitude: 0, longitude: 0);
private static readonly Location _northeast = new Location(latitude: RadianHalfPiToDegrees, longitude: RadianPiToDegrees);
private static readonly Location _southwest = new Location(latitude: -RadianHalfPiToDegrees, longitude: -RadianPiToDegrees);
private static Location GetValidLocation(double latitude, double longitude)
public class JoinPointExample
{
private const int Max = 10000000;
private static int SumDigits(int num)
{
int sum = 0;
while (num > 0)
{
@danzuep
danzuep / MergeSort.cs
Last active September 30, 2023 02:14
public static class ArrayExtensions
{
public static void MergeSort(this int[] array, int left, int right)
{
if (left >= right)
return;
var mid = (left + right) / 2;
MergeSort(array, left, mid);
MergeSort(array, mid + 1, right);
DoMerge(array, left, mid, right);
@danzuep
danzuep / EagerLoad.cs
Last active March 27, 2023 10:09
Class for asynchronous cached eager initialization of objects.
using System.Runtime.CompilerServices;
/// <summary>
/// EagerLoad<T> provides support for asynchronous cached eager initialization. This type is fully threadsafe.
/// Lazy<T> is for caching a value and synchronizing multiple threads attempting to get at that cached value.
/// Task<T> is for representing an asynchronous operation and making its result available in the future.
/// <see href="https://devblogs.microsoft.com/pfxteam/asynclazyt/"/>
/// <seealso href="https://blog.stephencleary.com/2012/08/asynchronous-lazy-initialization.html"/>
/// <seealso href="https://github.com/StephenCleary/AsyncEx/blob/master/src/Nito.AsyncEx.Coordination/AsyncLazy.cs"/>
/// </summary>
public class SslCertificateHelper
{
[SupportedOSPlatform("windows")]
public static X509Certificate2? GetX509Certificate2(string subject = "CN=localhost")
{
X509Certificate2? certificate = null;
using var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadWrite | OpenFlags.OpenExistingOnly);
var certs = certStore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, subject, false);
if (certs.Count > 0)
@danzuep
danzuep / ReadPasswordFromConsole
Last active April 3, 2023 08:45
Reads a password from the console, but only displays "*" characters.
private static string ReadPasswordFromConsole()
{
Console.TreatControlCAsInput = true;
var sb = new StringBuilder();
while (Console.ReadKey(true) is ConsoleKeyInfo cki && cki.Key != ConsoleKey.Enter)
{
if (cki.Modifiers == ConsoleModifiers.Control && cki.Key == ConsoleKey.C)
{
break;
}
public class StringExtensions
{
internal static string ReplaceFirstInstanceOf(this string? original, string oldValue, string newValue)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(original))
{
int index = original.IndexOf(oldValue);
result = index < 0 ? original : index == 0 ? string.Concat(newValue, original.AsSpan(oldValue.Length)) :
string.Concat(original.AsSpan(0, index), newValue, original.AsSpan(index + oldValue.Length));
public class DigitsOnly
{
public static string Get(string text)
{
string result = string.Empty;
if (!string.IsNullOrWhiteSpace(text))
{
var sb = new StringBuilder(text.Length);
for (int i = 0; i < text.Length; i++)
if (text[i] >= '0' && text[i] <= '9')
@danzuep
danzuep / Luhn.cs
Last active August 17, 2022 14:43
O(n) Luhn check
public class Luhn
{
public static bool CheckLuhnDigit(string digits, int last = 1)
{
var checksum = CalculateLuhnDigit(digits, last);
int checkDigit = checksum > -1 ? digits[^last] - '0' : 0;
return checksum == checkDigit;
}
public static int CalculateLuhnDigit(string digits, int lastIndex = 0)