Show / Hide Table of Contents

Class Utility

Static class containing extension helper methods for various built-in C# classes, as well as a static helper method for "swapping" references.

Inheritance
object
Utility
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: GoRogue
Assembly: GoRogue.dll
Syntax
public static class Utility

Methods

View Source

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) where TKey : notnull
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.

View Source

ExtendToStringGrid<T>(T[,], int, 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
int 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.

View Source

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.

View Source

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].

View Source

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).

View Source

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
View Source

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 = "}") where TKey : notnull
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}.

View Source

Flatten<T>(params 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
View Source

Multiply(string, int)

"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
int numTimes

The number of times to repeat the string.

Returns
Type Description
string

The current string repeated numTimes times.

View Source

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
View Source

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
View Source

Yield<T>(params 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
  • View Source
In This Article
Back to top Generated by DocFX