Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Runtime.CompilerServices;
// our code
MyType obj = new();
MyExtension ext = (MyExtension)obj;
ext.MyField = 42;
// other code
ext = (MyExtension)obj;
public sealed class SequenceEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public readonly IEqualityComparer<T> Comparer;
public SequenceEqualityComparer(IEqualityComparer<T>? comparer = null)
{
Comparer = comparer ?? EqualityComparer<T>.Default;
}
public static readonly SequenceEqualityComparer<T> Instance = new();
function inprint(table)
print(table)
for key, value in pairs(table) do
print('\t['..key..'] = ['..value..']')
end
end
@Sl4vP0weR
Sl4vP0weR / CapitalizeFirstLetter.cs
Created September 22, 2023 22:56
Capitalize first letter efficiently.
public static string? CapitalizeFirstLetter(this string? text)
{
if (string.IsNullOrWhiteSpace(text)) return text;
StringBuilder result = new(text);
var length = text!.Length;
for (var i = 0; i < length; i++)
{
var c = text[i];
@Sl4vP0weR
Sl4vP0weR / Language.cs
Created August 12, 2023 14:10
Language to culture implementation.
using System;
using System.Globalization;
public enum Language : int
{
Arabic = 1,
German = 7,
English = 9,
Spanish = 10,
French = 12,
using System;
using System.Linq;
using System.Collections.Generic;
/// <summary>
/// Enum extensions.
/// </summary>
public static class EnumExtensions
{
/// <inheritdoc cref="EnumExtensions{TEnum}.Values"/>
@Sl4vP0weR
Sl4vP0weR / ConditionalWeakDictionary.cs
Last active August 12, 2023 15:56
ConditionalWeakTable as ConditionalWeakDictionary.
public class ConditionalWeakDictionary<TKey, TValue> : IDictionary<TKey, TValue>
where TKey : class
where TValue : class
{
private readonly ConditionalWeakTable<TKey, TValue> table = new();
public TValue this[TKey key]
{
get => Get(key);
set => Set(key, value);
@Sl4vP0weR
Sl4vP0weR / OrderedQueue.cs
Created May 30, 2022 09:44
List with functionality of Queue that allows to enqueue elements in order using Comparison.
/// <summary>
/// <see cref="List{T}"/> with functionality of <see cref="Queue{T}"/> that allows to enqueue elements in order using <see cref="Comparison{T}"/>.
/// </summary>
/// <typeparam name="T">Type of elements.</typeparam>
public class OrderedQueue<T> : List<T>
{
public OrderedQueue(Comparison<T> comparison = null)
{
OrderComparison = comparison ?? ((a, b) => a.Equals(b) ? 0 : -1);
}
@Sl4vP0weR
Sl4vP0weR / Reverse_LinkedList_Extension.cs
Created May 9, 2022 08:20
Extension for reversing the linked list.
/// <summary>
/// Creates new <see cref="LinkedList{T}"/> with reversed nodes.
/// </summary>
/// <typeparam name="T"></typeparam>
public static LinkedList<T> Reverse<T>(this LinkedList<T> originalList)
{
var reversedList = new LinkedList<T>();
var last = originalList.Last;
reversedList.AddFirst(last.Value);
while ((last = last.Previous) is not null)
using System;
using System.Threading.Tasks;
public static class TaskExtensions
{
public delegate void ExceptionCallback(Exception exception);
public delegate void CompleteCallback<in T>(T result);
public delegate void CompleteCallback();
public static Task<T> WithResult<T>(this Task task, Func<Task, T> func) =>