Skip to content

01. Extensions Methods

David Shnayder edited this page Jul 26, 2024 · 4 revisions

Extension Methods

Collection Extensions

bool IsNullOrEmpty<T>(this ICollection<T>? collection);
Span<T> AsSpan<T>(this List<T> list);
ref TValue GetValueRefOrNullRef<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TKey : notnull {};
ref TValue? GetValueRefOrAddDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, out bool exists) where TKey : notnull {};
void CopyTo<TKey, TValue>(this Dictionary<TKey, TValue> dict, KeyValuePair<TKey,TValue>[] array, int index) : where TKey : notnull {};
(KeyValuePair<TKey, TValue>[] rentedBuffer, ArraySegment<KeyValuePair<TKey,TValue>> entries) RentBufferAndCopyEntries<TKey,TValue>(this Dictionary<TKey,TValue> dict) where TKey : notnull {};
void ReturnBufferToSharedArrayPool<T>(this T[] arr);
void ReturnBufferToArrayPool<T>(this T[] arr, ArrayPool<T> pool);
T[] PureSort<T>(this T[] source, IComparer<T> comparer);
List<T> PureSort<T>(this IEnumerable<T> source, IComparer<T> comparer);
void RemoveDuplicatesFromSorted<T>(this List<T> list, IComparer<T> comparer);
void RemoveDuplicates<T>(this List<T> list, IEqualityComparer<T>? comparer = null, bool isSorted = false);
void RemoveDuplicates<T>(this List<T> list, out HashSet<T> hSet, IEqualityComparer<T>? comparer = null, bool isSorted = false);
void RemoveDuplicatesSorted<T>(this List<T> list, IEqualityComparer<T>? comparer);
void RemoveDuplicatesNotSorted<T>(this List<T> list, IEqualityComparer<T>? comparer, out HashSet<T> hSet);
List<ArraySegment<T>> ChunkToSegments<T>(this T[] arr, int sizeOfChunk);
int CopyToArray<T>(this HashSet<T> hashSet, T[] destination, int index);

DateTime Extensions

// Formats the TimeSpan to show the largest unit with 2 decimal places and the unit name
// i.e 5.15 ms
string Format(this TimeSpan elapsed);
ReadOnlySpan<char> FormatNonAllocated(this TimeSpan elapsed, Span<char> buffer);
ReadOnlyMemory<char> FormatNonAllocated(this TimeSpan elapsed, char[] buffer);
AllocatedStringBuffer FormatNonAllocated(this TimeSpan elapsed, char[] buffer, Span<char> sBuffer);
// Formats the TimeSpan to show each of the units as whole numbers with the corresponding unit names
// i.e 4d 3h 1m 57s
string ToRemainingDuration(this TimeSpan time);
ReadOnlySpan<char> ToRemainingDurationNonAllocated(this TimeSpan time, Span<char> buffer);
ReadOnlyMemory<char> ToRemainingDurationNonAllocated(this TimeSpan time, char[] buffer);
AllocatedStringBuffer ToRemainingDurationNonAllocated(this TimeSpan time, char[] buffer, Span<char> sBuffer);
// Formats the string to HHMM-dd-mmm-yy
string ToTimeStamp(this DateTime time);
ReadOnlySpan<char> ToTimeStampNonAllocated(this DateTime time, Span<char> buffer);
ReadOnlyMemory<char> ToTimeStampNonAllocated(this DateTime time, char[] buffer);
AllocatedStringBuffer ToTimeStampNonAllocated(this DateTime time, char[] buffer, Span<char> sBuffer);

Unmanaged Extensions

bool TryParseAsEnum<TEnum>(this string value, out TEnum result) where TEnum : struct, Enum;

String Extensions

ref char GetReference(this string text); // Returns actual reference to first character
bool IsNullOrEmpty(this string str);
bool IsNullOrWhiteSpace(this string str);
// Tried to convert the string to int32 using ascii, very efficient
bool TryConvertToInt32(this ReadOnlySpan<char> value, out int result);
// Concat - more efficient than + or interpolation
string Concat(this string value, ReadOnlySpan<char> suffix);
string ToTitle(this string str);
bool IsBinary(this string str);

Clone this wiki locally