Class ToStringExtensions
Static class containing extension extension methods for printing various built-in C# classes (as well as some grid views) as strings neatly.
Useful for debugging, and within the primitives library for ToString implementations.
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public static class ToStringExtensions : Object
Methods
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.