Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Static class: RT.Util.Ut

Summary

Provides extension methods that apply to all types.

Static methods

byte[]
AesDecrypt(byte[] cipher, byte[] key)
Decrypts data encrypted with Ut.AesEncrypt(byte[], byte[]).
byte[]
AesEncrypt(byte[] plain, byte[] key)
Encrypts bytes using AES in CBC mode.
void
Apply<TSource>(this TSource source, Action<TSource> action)
Executes the specified action with the specified argument.
TResult
Apply<TSource, TResult>(this TSource source, Func<TSource, TResult> func)
Executes the specified function with the specified argument.
void
Apply<TSource1, TSource2>(this ValueTuple<TSource1, TSource2> source, Action<TSource1, TSource2> action)
Executes the specified action with the specified tuple argument.
TResult
Apply<TSource1, TSource2, TResult>(this ValueTuple<TSource1, TSource2> source, Func<TSource1, TSource2, TResult> func)
Executes the specified function with the specified tuple argument.
T[]
ArrayConcat<T>(params T[][] arrays)
Concatenates any number of arrays. (see also remarks)
int
ArrayHash(params object[] input)
Computes a hash value from an array of elements.
int
ArrayHash(Array input)
void
Assert(bool assertion, string message = null)
Checks the specified condition and causes the debugger to break if it is false. Throws an Exception afterwards.
void
AssertAll<T>(IEnumerable<T> collection, Func<T, bool> assertion, string message = null)
int
BytesToInt(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 32-bit signed integer.
long
BytesToLong(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 64-bit signed integer.
short
BytesToShort(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 16-bit signed integer.
uint
BytesToUInt(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 32-bit unsigned integer.
ulong
BytesToULong(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 64-bit unsigned integer.
ushort
BytesToUShort(byte[] buffer, int index, bool bigEndian = false)
Converts the bytes at the specified index within the specified buffer to a 16-bit unsigned integer.
Delegate
CreateDelegate(object firstArgument, MethodInfo method)
Creates a delegate using Action<,*> or Func<,*> depending on the number of parameters of the specified method.
void
Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> source, out TKey key, out TValue value)
Allows the deconstruction of KeyValuePairs into separate variables.
IEnumerable<ValueTuple<T, DiffOp>>
Diff<T>(IEnumerable<T> old, IEnumerable<T> new, IEqualityComparer<T> comparer = null, Func<T, bool> predicate = null, Func<IEnumerable<T>, IEnumerable<T>, IEnumerable<ValueTuple<T, DiffOp>>> postProcessor = null)
Computes a representation of the differences between old and new using the specified options.
string
FormatCsvRow(IEnumerable<object> cells)
Formats an entire CSV row, escaping cell values where required. See Remarks. (see also remarks)
string
FormatCsvRow(params object[] cells)
int
Gcd(int a, int b)
Calculates the greatest common divisor of a and b.
uint
Gcd(uint a, uint b)
long
Gcd(long a, long b)
ulong
Gcd(ulong a, ulong b)
TAttribute
GetCustomAttribute<TAttribute>(this Enum enumValue)
Returns the single custom attribute of the specified TAttribute type that is attached to the declaration of the enum value represented by enumValue, or null if there is no such attribute. (see also remarks)
TAttribute[]
GetCustomAttributes<TAttribute>(this Enum enumValue)
Returns the set of custom attributes of the specified TAttribute type that are attached to the declaration of the enum value represented by enumValue. (see also remarks)
T
GetFieldValue<T>(this object instance, string fieldName)
Searches the specified object’s type for a field of the specified name and returns that field’s value. (see also remarks)
string
GetLongestCommonSubstring(params string[] strings)
Finds the longest substring that all of the specified input strings contain.
bool
IsDefault<T>(this T val)
Returns true if this value is equal to the default value for this type.
KeyValuePair<TKey, TValue>
KeyValuePair<TKey, TValue>(TKey key, TValue value)
Allows the use of type inference when creating .NET’s KeyValuePair<TK,TV>.
Action
Lambda(Action method)
Allows the use of C#’s powerful type inference when declaring local lambdas whose delegate type doesn't make any difference.
Action<T>
Lambda<T>(Action<T> method)
Func<TResult>
Lambda<TResult>(Func<TResult> method)
Action<T1, T2>
Lambda<T1, T2>(Action<T1, T2> method)
Func<T, TResult>
Lambda<T, TResult>(Func<T, TResult> method)
Action<T1, T2, T3>
Lambda<T1, T2, T3>(Action<T1, T2, T3> method)
Func<T1, T2, TResult>
Lambda<T1, T2, TResult>(Func<T1, T2, TResult> method)
Action<T1, T2, T3, T4>
Lambda<T1, T2, T3, T4>(Action<T1, T2, T3, T4> method)
Func<T1, T2, T3, TResult>
Lambda<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> method)
Action<T1, T2, T3, T4, T5>
Lambda<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5> method)
Func<T1, T2, T3, T4, TResult>
Lambda<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> method)
Func<T1, T2, T3, T4, T5, TResult>
Lambda<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, TResult> method)
T
Max<T>(params T[] args)
Returns the largest of all arguments passed in. Uses the Linq .Max extension method to do the work.
T
Max<T>(T val1, T val2)
Returns the larger of the two IComparable values. If the values are equal, returns the first one.
T
Max<T>(T val1, T val2, T val3)
Returns the larger of the three IComparable values. If two values are equal, returns the earlier one.
string
Md5(string path)
Reads the specified file and computes the MD5 hash function from its contents.
T
Min<T>(params T[] args)
Returns the smallest of all arguments passed in. Uses the Linq .Min extension method to do the work.
T
Min<T>(T val1, T val2)
Returns the smaller of the two IComparable values. If the values are equal, returns the first one.
T
Min<T>(T val1, T val2, T val3)
Returns the smaller of the three IComparable values. If two values are equal, returns the earlier one.
T[]
NewArray<T>(params T[] parameters)
Returns the parameters as a new array. (see also remarks)
T[]
NewArray<T>(int size, Func<int, T> initialiser)
Instantiates a fully-initialized array with the specified dimensions.
T[][]
NewArray<T>(int size1, int size2, Func<int, int, T> initialiser = null)
Instantiates a fully-initialized rectangular jagged array with the specified dimensions.
T[][][]
NewArray<T>(int size1, int size2, int size3, Func<int, int, int, T> initialiser = null)
Instantiates a fully-initialized "rectangular" jagged array with the specified dimensions.
TInput?
Nullable<TInput>(this TInput input)
Turns the specified value into a nullable value.
TResult
NullOr<TInput, TResult>(this TInput input, FuncClassClass<TInput, TResult> lambda)
Returns null if the input is null, otherwise the result of the specified lambda when applied to the input.
TResult?
NullOr<TInput, TResult>(this TInput input, FuncClass<TInput, TResult?> lambda)
TResult?
NullOr<TInput, TResult>(this TInput input, FuncClassStruct<TInput, TResult> lambda)
TResult
NullOr<TInput, TResult>(this TInput? input, FuncStructClass<TInput, TResult> lambda)
TResult?
NullOr<TInput, TResult>(this TInput? input, FuncStruct<TInput, TResult?> lambda)
TResult?
NullOr<TInput, TResult>(this TInput? input, FuncStructStruct<TInput, TResult> lambda)
TResult
OnException<TException, TResult>(Func<TResult> func, Func<TException, TResult> onException)
Evaluates the specified code and returns its result. If the code throws an exception of the specified type, runs onException instead.
TResult
OnExceptionDefault<TResult>(Func<TResult> func, TResult default)
Evaluates the specified code and returns its result. If the code throws any exceptions, returns default instead.
TResult
OnExceptionDefault<TResult, TException>(Func<TResult> func, TResult default)
Evaluates the specified code and returns its result. If the code throws TException, returns default instead. Doesn't catch any other exceptions.
bool
OnExceptionIgnore(Action action)
Evaluates the specified code. If the code throws any exceptions, catches and suppresses them.
bool
OnExceptionIgnore<TException>(Action action)
Evaluates the specified code and returns its result. If the code throws TException, catches and suppresses it. Doesn't catch any other exceptions.
void
OnExceptionRetry(Action action, int attempts = 3, int delayMs = 333, Action onException = null)
Evaluates the specified code. If the code throws any exceptions, retries the specified number of times. The final attempt is executed without any exception handlers.
void
OnExceptionRetry<TException>(Action action, int attempts = 3, int delayMs = 333)
Evaluates the specified code. If the code throws TException, retries the specified number of times. The final attempt is executed without any exception handlers.
TResult
OnExceptionRetry<TResult>(Func<TResult> func, int attempts = 3, int delayMs = 333, Action onException = null)
Evaluates the specified code and returns its result. If the code throws any exceptions, retries the specified number of times. The final attempt is executed without any exception handlers.
TResult
OnExceptionRetry<TResult, TException>(Func<TResult> func, int attempts = 3, int delayMs = 333)
Evaluates the specified code and returns its result. If the code throws TException, retries the specified number of times. The final attempt is executed without any exception handlers.
TResult
OnExceptionRetryThenDefault<TResult>(Func<TResult> func, TResult default, int attempts = 3, int delayMs = 333)
Evaluates the specified code and returns its result. If the code throws any exceptions, retries the specified number of times. If the code still throws on the final attempt, returns default instead.
TResult
OnExceptionRetryThenDefault<TResult, TException>(Func<TResult> func, TResult default, int attempts = 3, int delayMs = 333)
Evaluates the specified code and returns its result. If the code throws TException, retries the specified number of times. If the code still throws TException on the final attempt, returns default instead. Doesn't catch any other exceptions.
bool
OnExceptionRetryThenIgnore(Action action, int attempts = 3, int delayMs = 333)
Evaluates the specified code. If the code throws any exceptions, retries the specified number of times. If the code still throws on the final attempt, suppresses the exception.
bool
OnExceptionRetryThenIgnore<TException>(Action action, int attempts = 3, int delayMs = 333)
Evaluates the specified code. If the code throws TException, retries the specified number of times. If the code still throws TException on the final attempt, suppresses the exception. Doesn't catch any other exceptions.
void
Parallel(params Action<int>[] actions)
Runs all of the specified actions in parallel, each in a thread of its own.
void
Parallel(int maxSimultaneous, params Action<int>[] actions)
Runs the specified actions partly in parallel by using no more than the specified maximum number of threads.
void
ParallelForEach<T>(this IEnumerable<T> items, Action<T> action)
Runs the specified action in parallel for each item in the input collection.
void
ParallelForEach<T>(this IEnumerable<T> items, Action<T, int> action)
void
ParallelForEach<T>(this IEnumerable<T> items, int maxSimultaneous, Action<T> action)
Runs the specified action in parallel for each item in the input collection, using no more than the specified maximum number of threads.
void
ParallelForEach<T>(this IEnumerable<T> items, int maxSimultaneous, Action<T, int> action)
IEnumerable<TResult>
ParallelSelect<TSource, TResult>(this IEnumerable<TSource> items, Func<TSource, TResult> selector)
Runs the specified function in parallel for each item in the input collection and returns a collection containing the results of the function calls.
IEnumerable<TResult>
ParallelSelect<TSource, TResult>(this IEnumerable<TSource> items, int maxSimultaneous, Func<TSource, TResult> selector)
IEnumerable<TResult>
ParallelSelectMany<TSource, TResult>(this IEnumerable<TSource> items, Func<TSource, IEnumerable<TResult>> selector)
Runs the specified function in parallel for each item in the input collection and returns a collection containing the concatenation of all the results of the function calls.
IEnumerable<TResult>
ParallelSelectMany<TSource, TResult>(this IEnumerable<TSource> items, int maxSimultaneous, Func<TSource, IEnumerable<TResult>> selector)
IEnumerable<string[]>
ParseCsv(TextReader reader, int minColumns = 0)
Parses text from the reader as CSV. See Remarks. (see also remarks)
IEnumerable<string[]>
ParseCsv(string content, int minColumns = 0)
Parses content as CSV. For details see Ut.ParseCsv(TextReader, int).
IEnumerable<string[]>
ParseCsvFile(string path, int minColumns = 0)
Parses the file at path as CSV. For details see Ut.ParseCsv(TextReader, int).
DateTime?
ParseDateTime(string value)
Returns the date/time stamp represented by the specified string, or null if the string does not represent a valid date/time stamp.
double?
ParseDouble(string value)
Returns the floating-point number represented by the specified string, or null if the string does not represent a valid double-precision floating-point number.
T?
ParseEnum<T>(string value, bool ignoreCase = false)
Returns the enum value represented by the specified string, or null if the string does not represent a valid enum value.
int?
ParseInt32(string value)
Returns the integer represented by the specified string, or null if the string does not represent a valid 32-bit integer.
long?
ParseInt64(string value)
Returns the integer represented by the specified string, or null if the string does not represent a valid 64-bit integer.
IEnumerable<double>
Range(double start, double end, double step)
Generates a sequence of floating-point numbers within a specified range.
IEnumerable<T>
ReduceRequiredSet<T>(IEnumerable<T> items, Func<Ut.ReduceRequiredSetState<T>, bool> test, bool breadthFirst = false, bool skipConsistencyTest = false)
Given a set of values and a function that returns true when given this set, will efficiently remove items from this set which are not essential for making the function return true. The relative order of items is preserved. This method cannot generally guarantee that the result is optimal, but for some types of functions the result will be guaranteed optimal.
void
RunMain(Action main, Action<Exception> onUnhandledMain, Action<Exception> onUnhandledThread = null)
Execute the core of the application. In debug mode, exceptions are passed through untouched. In release mode, all exceptions are caught, including those on other threads, and passed on to the specified handlers. Also, the main thread is given the name "Main".
int
RunMain(Func<int> main, Func<Exception, int> onUnhandledMain, Action<Exception> onUnhandledThread = null)
string
Sha1(string path)
Reads the specified file and computes the SHA1 hash function from its contents.
string
SizeToString(long size)
Converts file size in bytes to a string that uses KB, MB, GB or TB.
void
Swap<T>(ref T one, ref T two)
Swaps the values of the specified two variables.
void
ThreadPool(Action task)
Queues the specified action to be executed on the thread pool. This is just a shortcut for ThreadPool.QueueUserWorkItem, and also does not require the method to accept a parameter (which has been useless ever since C# gained support for lambdas).
double
Tic()
Starts / resets a simple performance timer. Returns the number of seconds elapsed since the last call to Ut.Tic(), or zero if this is the first call. See also Ut.Toc().
double
Toc()
Returns the number of seconds elapsed since the last call to Ut.Tic().
string
ToHex(this byte[] data)
Generates a representation of the specified byte array as hexadecimal numbers (“hexdump”).
string
ToHex(this uint[] data)
Generates a representation of the specified uint array as hexadecimal numbers (“hexdump”).
string
ToHex(this IEnumerable<byte> data, int spacesEvery = 0)
Generates a representation of the specified byte sequence as hexadecimal numbers (“hexdump”).
stringReturns the version of the entry assembly (the .exe file) in a standard format.
void
WaitSharingVio(Action action, TimeSpan? maximum = null, Action onSharingVio = null)
Executes the specified action. If the action results in a file sharing violation exception, the action will be repeatedly retried after a short delay (which increases after every failed attempt).
T
WaitSharingVio<T>(Func<T> func, TimeSpan? maximum = null, Action onSharingVio = null)
Executes the specified function. If the function results in a file sharing violation exception, the function will be repeatedly retried after a short delay (which increases after every failed attempt).

Nested types

Encapsulates the state of the Ut.ReduceRequiredSet<T>(IEnumerable<T>, Func<Ut.ReduceRequiredSetState<T>, bool>, bool, bool) algorithm and exposes statistics about it.