Skip to content

Instantly share code, notes, and snippets.

@pmswe
pmswe / 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,
@pmswe
pmswe / 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];
function inprint(table)
print(table)
for key, value in pairs(table) do
print('\t['..key..'] = ['..value..']')
end
end
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();
using System;
using System.Runtime.CompilerServices;
// our code
MyType obj = new();
MyExtension ext = (MyExtension)obj;
ext.MyField = 42;
// other code
ext = (MyExtension)obj;
public class MethodSignatureComparer : IEqualityComparer<ParameterInfo>
{
public static readonly MethodSignatureComparer Default = new();
public bool Match(MethodInfo signatureMethod, MethodInfo otherMethod)
{
var signatureParameters = signatureMethod.GetParameters();
var parameters = otherMethod.GetParameters();
if (otherMethod.Name != signatureMethod.Name)
@pmswe
pmswe / StreamWrapper.cs
Created August 31, 2025 18:25
Thread-safe stream wrapper that discards certain operations on the underlying stream as a lifetime control measure.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Thread-safe stream wrapper that discards certain operations on the underlying stream as a lifetime control measure.<br/>
/// If intention is only to read, external code should only read from the stream, other unintended operations will be discarded.<br/>
/// In case external code tries to manage lifetime of the stream, on <see cref="Stream.Dispose()"/> the underlying stream isn't being disposed.
/// </summary>