Class Utility
Static class containing extension helper methods for various built-in C# classes, as well as a static helper method for "swapping" references.
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public static class Utility : Object
Methods
AsReadOnly<TKey, TValue>(IDictionary<TKey, TValue>)
Adds an AsReadOnly method to IDictionary<TKey,TValue>, similar to the AsReadOnly method of IList<T>, that returns a read-only reference to the dictionary.
Declaration
public static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary |
Returns
Type | Description |
---|---|
ReadOnlyDictionary<TKey, TValue> | A ReadOnlyDictionary instance for the specified dictionary. |
Type Parameters
Name | Description |
---|---|
TKey | Type of keys of the dictionary. |
TValue | Type of values of the dictionary. |
ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this T[, ] array, string begin = "[\n", string beginRow = "\t[", Func<T, string> elementStringifier = null, string rowSeparator = ",\n", string elementSeparator = ", ", string endRow = "]", string end = "\n]")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
String | begin | Character(s) that should precede the string representation of the 2D array. |
String | beginRow | Character(s) that should precede the string representation of each row. |
Func<T, String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
String | rowSeparator | Character(s) used to separate each row from the next. |
String | elementSeparator | Character(s) used to separate each element from the next. |
String | endRow | Character(s) that should follow the string representation of each row. |
String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
String | A string representation of the 2D array. |
Type Parameters
Name | Description |
---|---|
T |
ExtendToString<T>(IEnumerable<T>, String, Func<T, String>, String, String)
Extension method for IEnumerable<T> that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this IEnumerable<T> enumerable, string begin = "[", Func<T, string> elementStringifier = null, string separator = ", ", string end = "]")
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | enumerable | |
String | begin | Character(s) that should precede the string representation of the IEnumerable's elements. |
Func<T, String> | elementStringifier | Function to use to get the string representation of each element. Specifying null uses the ToString function of type T. |
String | separator | Characters to separate the IEnumerable's elements by. |
String | end | Character(s) that should follow the string representation of the IEnumerable's elements. |
Returns
Type | Description |
---|---|
String | A string representation of the IEnumerable. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Built-in C# data structures like List<T> implement IEnumerable<T>, and as such this method can be used to stringify the contents of C# built-in data structures. When no customization parameters are specified, it defaults to a representation looking something like [elem1, elem2, elem3].
ExtendToString<T>(ISet<T>, String, Func<T, String>, String, String)
Extension method for ISet<T> that allows retrieving a string representing the contents.
Declaration
public static string ExtendToString<T>(this ISet<T> set, string begin = "set(", Func<T, string> elementStringifier = null, string separator = ", ", string end = ")")
Parameters
Type | Name | Description |
---|---|---|
ISet<T> | set | |
String | begin | Character(s) that should precede the string representation of the set's elements. |
Func<T, String> | elementStringifier | Function to use to get the string representation of each element. Specifying null uses the ToString function of type T. |
String | separator | Characters to separate the set's items by. |
String | end | Character(s) that should follow the string representation of the set's elements. |
Returns
Type | Description |
---|---|
String | A string representation of the ISet. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Built-in C# data structures like HashSet<T> implement ISet<T>, and as such this method can be used to stringify the contents of C# built-in set structures. When no customization parameters are specified, it defaults to a representation looking something like set(elem1, elem2, elem3).
ExtendToString<TKey, TValue>(IDictionary<TKey, TValue>, String, Func<TKey, String>, Func<TValue, String>, String, String, String)
Extension method for dictionaries that allows retrieving a string representing the dictionary's contents.
Declaration
public static string ExtendToString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, string begin = "{", Func<TKey, string> keyStringifier = null, Func<TValue, string> valueStringifier = null, string kvSeparator = " : ", string pairSeparator = ", ", string end = "}")
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TKey, TValue> | dictionary | |
String | begin | Character(s) that should precede the string representation of the dictionary's elements. |
Func<TKey, String> | keyStringifier | Function to use to get the string representation of each key. Specifying null uses the ToString function of type K. |
Func<TValue, String> | valueStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type V. |
String | kvSeparator | Characters used to separate each value from its key. |
String | pairSeparator | Characters used to separate each key-value pair from the next. |
String | end | Character(s) that should follow the string representation of the dictionary's elements. |
Returns
Type | Description |
---|---|
String | A string representation of the IDictionary. |
Type Parameters
Name | Description |
---|---|
TKey | |
TValue |
Remarks
Built-in C# data structures like Dictionary<TKey,TValue> implement IDictionary<TKey,TValue>, and as such this method can be used to stringify the contents of C# built-in dictionary structures. When no customization parameters are specified, it defaults to a representation looking something like {key1 : value, key2 : value}.
ExtendToStringGrid<T>(T[,], Int32, String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents, formatted as if the 2D array represents a coordinate plane/grid.
Declaration
public static string ExtendToStringGrid<T>(this T[, ] array, int fieldSize, string begin = "", string beginRow = "", Func<T, string> elementStringifier = null, string rowSeparator = "\n", string elementSeparator = " ", string endRow = "", string end = "")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
Int32 | fieldSize | The amount of space each element should take up in characters. A positive number aligns the text to the right of the space, while a negative number aligns the text to the left. |
String | begin | Character(s) that should precede the string representation of the 2D array. |
String | beginRow | Character(s) that should precede the string representation of each row. |
Func<T, String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
String | rowSeparator | Character(s) used to separate each row from the next. |
String | elementSeparator | Character(s) used to separate each element from the next. |
String | endRow | Character(s) that should follow the string representation of each row. |
String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
String | A string representation of the 2D array, formatted as if the array represents a 2D coordinate plane/grid map. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
This differs from ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String) in that this method prints the array such that array[x+1, y] is printed to the RIGHT of array[x, y], rather than BELOW it. Effectively it assumes the indexes being used are grid/coordinate plane coordinates.
ExtendToStringGrid<T>(T[,], String, String, Func<T, String>, String, String, String, String)
Extension method for 2D arrays that allows retrieving a string representing the contents, formatted as if the 2D array represents a coordinate plane/grid.
Declaration
public static string ExtendToStringGrid<T>(this T[, ] array, string begin = "", string beginRow = "", Func<T, string> elementStringifier = null, string rowSeparator = "\n", string elementSeparator = " ", string endRow = "", string end = "")
Parameters
Type | Name | Description |
---|---|---|
T[,] | array | |
String | begin | Character(s) that should precede the string representation of the 2D array. |
String | beginRow | Character(s) that should precede the string representation of each row. |
Func<T, String> | elementStringifier | Function to use to get the string representation of each value. Specifying null uses the ToString function of type T. |
String | rowSeparator | Character(s) used to separate each row from the next. |
String | elementSeparator | Character(s) used to separate each element from the next. |
String | endRow | Character(s) that should follow the string representation of each row. |
String | end | Character(s) that should follow the string representation of the 2D array. |
Returns
Type | Description |
---|---|
String | A string representation of the 2D array, formatted as if the array represents a 2D coordinate plane/grid map. |
Type Parameters
Name | Description |
---|---|
T |
Remarks
This differs from ExtendToString<T>(T[,], String, String, Func<T, String>, String, String, String, String) in that this method prints the array such that array[x+1, y] is printed to the RIGHT of array[x, y], rather than BELOW it. Effectively it assumes the indexes being used are grid/coordinate plane coordinates.
Flatten<T>(IEnumerable<T>[])
Takes multiple enumerables of items, and flattens them into a single IEnumerable.
Declaration
public static IEnumerable<T> Flatten<T>(params IEnumerable<T>[] lists)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T>[] | lists | Lists to "flatten". |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable containing the items of all the enumerables passed in. |
Type Parameters
Name | Description |
---|---|
T |
Multiply(String, Int32)
"Multiplies", aka repeats, a string the given number of times.
Declaration
public static string Multiply(this string str, int numTimes)
Parameters
Type | Name | Description |
---|---|---|
String | str | |
Int32 | numTimes | The number of times to repeat the string. |
Returns
Type | Description |
---|---|
String | The current string repeated |
Swap<T>(ref T, ref T)
Swaps the values pointed to by lhs
and rhs
.
Declaration
public static void Swap<T>(ref T lhs, ref T rhs)
Parameters
Type | Name | Description |
---|---|---|
T | lhs | |
T | rhs |
Type Parameters
Name | Description |
---|---|
T |
Yield<T>(T)
Convenience function that yields the given item as a single-item IEnumerable.
Declaration
public static IEnumerable<T> Yield<T>(this T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable containing only the item the function is called on. |
Type Parameters
Name | Description |
---|---|
T |
Yield<T>(T[])
Takes multiple parameters and converts them to an IEnumerable.
Declaration
public static IEnumerable<T> Yield<T>(params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | Parameters (specified as multiple parameters to the function). |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable of all of the given items, in the order they were given to the function. |
Type Parameters
Name | Description |
---|---|
T |