Skip to content

02. Utilities

David Shnayder edited this page Jun 3, 2024 · 2 revisions

Utils

Utils is a static class that provides advanced options that wouldn't be straight forward as extensions, it has static subclasses that are classified by their area of operation.

Utils.DateAndTime

// Returns the date time as value task that can be fired and awaited to be later used, removing the need to synchronously wait for it
ValueTask<DateTime> GetCurrentTimeAsync();
// Same but returns the binary representation
ValueTask<long> GetCurrentTimeInBinaryAsync();

Utils.Env

bool IsRunningOnWindows();
bool IsRunningAsAdmin();
string GetBaseDirectory();
bool IsInternetAvailable;
string PathInBaseDirectory(string filename); // Returns a filename combined with the base directory
void OpenLink(string url); // semi-cross-platform (works on Windows, Mac and Linux)

Utils.Mathematics

double RollingAverage(double oldAverage, double newNumber, int sampleCount);
double Factorial(double n);
double FibonacciApproximation(int n);

Utils.Strings

// Format bytes into to a text containing the largest storage unit with 2 decimals places and the storage unit
// i.e 5.23 MB or 6.77 TB and so on...
string FormatBytes(long bytes);
string FormatBytes(double bytes);
ReadOnlySpan<char> FormatBytesNonAllocated(long bytes, Span<char> buffer);
ReadOnlyMemory<char> FormatBytesNonAllocated(long bytes, char[] buffer);
ReadOnlySpan<char> FormatBytesNonAllocated(double bytes, Span<char> buffer);
ReadOnlyMemory<char> FormatBytesNonAllocated(double bytes, char[] buffer);
AllocatedStringBuffer FormatBytesNonAllocated(double bytes, Span<char> buffer, Span<char> sBuffer);

Utils.Unsafe

Func<T, int> CreateIntegerPredicate<T>(Func<T, bool> predicate);
unsafe Span<T> AsMutableSpan<T>(ReadOnlySpan<T> span);
bool TryUnbox<T>(object obj, out T value) where T : struct;

Clone this wiki locally