This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JoinPointExample | |
{ | |
private const int Max = 10000000; | |
private static int SumDigits(int num) | |
{ | |
int sum = 0; | |
while (num > 0) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |