Class HelperExtensionsCommon
A collection of extension methods for various default types
Inherited Members
Namespace: BionicCode.Utilities.Net
Assembly: BionicCode.Utilities.Net.Common.dll
Syntax
public static class HelperExtensionsCommon
Methods
| Improve this Doc View SourceAddColumn<TData>(DataTable, String)
Adds a new DataColumn to a DataTable.
Declaration
public static void AddColumn<TData>(this DataTable source, string columnName)
Parameters
| Type | Name | Description |
|---|---|---|
| DataTable | source | The DataTable to add the new DataColumn to. |
| String | columnName | The column's name. |
Type Parameters
| Name | Description |
|---|---|
| TData | The data type of the column value. |
Remarks
Added columns are initialized to the default value of TData for all existing rows.
AddColumn<TData>(DataTable, String, Int32) should be called before an overload of AddRow(DataTable, Object[])
AddColumn<TData>(DataTable, String, Int32)
Adds a new DataColumn to a DataTable.
Declaration
public static void AddColumn<TData>(this DataTable source, string columnName, int columnIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| DataTable | source | The DataTable to add the new DataColumn to. |
| String | columnName | The column's name. |
| Int32 | columnIndex | The explicit index of the created DataColumn. |
Type Parameters
| Name | Description |
|---|---|
| TData | The data type of the column value. |
Remarks
If columnIndex is less than 0 or greater than the existing number of columns - 1 (greater than the ordinal of the last column) then an Invalid ArgumentException is thrown.
Any columns between the previous and new ordinal will be renumbered, to adjust for a column's new ordinal.
Added columns are initialized to the default value ofTData for all existing rows.
AddColumn<TData>(DataTable, String, Int32) should be called before an overload of AddRow(DataTable, Object[])
AddRange<TItem>(ICollection<TItem>, IEnumerable<TItem>)
Adds a range of items to the ICollection<T>.
Declaration
public static IEnumerable<TItem> AddRange<TItem>(this ICollection<TItem> source, IEnumerable<TItem> range)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<TItem> | source | The ICollection<T> to modify. |
| IEnumerable<TItem> | range | The items to add. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TItem> | The original IEnumerable<T> this method was invoked on to allow method chaining. |
Type Parameters
| Name | Description |
|---|---|
| TItem | The type of the item. |
Remarks
Although this method returns a IEnumerable<T> it modifies the original collection. The value is only returned to enable method chaining.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
AddRange<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)
Adds a IDictionary<TKey, TValue> to the IDictionary<TKey, TValue>.
Declaration
public static IDictionary<TKey, TValue> AddRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IDictionary<TKey, TValue> range)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | source | The IDictionary<TKey, TValue> to modify. |
| IDictionary<TKey, TValue> | range | The IDictionary<TKey, TValue> to add. |
Returns
| Type | Description |
|---|---|
| IDictionary<TKey, TValue> | The original IDictionary<TKey, TValue> this method was invoked on to allow method chaining. |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of the key. |
| TValue | The type of the value. |
Remarks
Although this method returns a IDictionary<TKey, TValue> it modifies the original collection. The value is only returned to enable method chaining.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
AddRange<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<KeyValuePair<TKey, TValue>>)
Adds a range of IEnumerable<KeyValuePair<TKey,TValue>> to the IDictionary<TKey, TValue>.
Declaration
public static IDictionary<TKey, TValue> AddRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IEnumerable<KeyValuePair<TKey, TValue>> range)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | source | The IDictionary<TKey, TValue> to modify. |
| IEnumerable<KeyValuePair<TKey, TValue>> | range | The |
Returns
| Type | Description |
|---|---|
| IDictionary<TKey, TValue> | The original IDictionary<TKey, TValue> this method was invoked on to allow method chaining. |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of the key. |
| TValue | The type of the value. |
Remarks
Although this method returns a IDictionary<TKey, TValue> it modifies the original collection. The value is only returned to enable method chaining.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
AddRange<TKey, TValue>(IDictionary<TKey, TValue>, IEnumerable<(TKey Key, TValue Value)>)
Adds a range of IEnumerable<(TKey Key,TValue Value)> to the IDictionary<TKey, TValue>.
Declaration
public static IDictionary<TKey, TValue> AddRange<TKey, TValue>(this IDictionary<TKey, TValue> source, IEnumerable<(TKey Key, TValue Value)> range)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | source | The IDictionary<TKey, TValue> to modify. |
| IEnumerable<System.ValueTuple<TKey, TValue>> | range | The |
Returns
| Type | Description |
|---|---|
| IDictionary<TKey, TValue> | The original IDictionary<TKey, TValue> this method was invoked on to allow method chaining. |
Type Parameters
| Name | Description |
|---|---|
| TKey | The type of the key. |
| TValue | The type of the value. |
Remarks
Although this method returns a IDictionary<TKey, TValue> it modifies the original collection. The value is only returned to enable method chaining.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
AddRow(DataTable, IEnumerable<Object>)
Declaration
public static void AddRow(this DataTable source, IEnumerable<object> columnValues)
Parameters
| Type | Name | Description |
|---|---|---|
| DataTable | source | |
| IEnumerable<Object> | columnValues | The ordered row's cell values, where the item index maps to the column index. |
Exceptions
| Type | Condition |
|---|---|
| IndexOutOfRangeException | When |
AddRow(DataTable, Object[])
Declaration
public static void AddRow(this DataTable source, params object[] columnValues)
Parameters
| Type | Name | Description |
|---|---|---|
| DataTable | source | |
| Object[] | columnValues | The row's cell values ordered by column index. |
AddRow(DataTable, (Int32 ColumnIndex, Object ColumnValue)[])
Declaration
public static void AddRow(this DataTable source, params (int ColumnIndex, object ColumnValue)[] columnValues)
Parameters
| Type | Name | Description |
|---|---|---|
| DataTable | source | |
| System.ValueTuple<Int32, Object>[] | columnValues | The row's cell values passed as tuples of |
Exceptions
| Type | Condition |
|---|---|
| IndexOutOfRangeException | When |
HasReachedEnd(Stream)
Return whether the end of a Stream is reached.
Declaration
public static bool HasReachedEnd(this Stream streamToCheck)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | streamToCheck |
Returns
| Type | Description |
|---|---|
| Boolean |
IsEmpty<TItem>(IEnumerable<TItem>)
Determines whether a sequence is empty.
Declaration
public static bool IsEmpty<TItem>(this IEnumerable<TItem> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TItem> | source |
Returns
| Type | Description |
|---|---|
| Boolean | true if |
Type Parameters
| Name | Description |
|---|---|
| TItem |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
LastInSorted<TItem>(IEnumerable<TItem>, Func<TItem, Boolean>)
A non-cached version of Last<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) for ascending sorted collections.
Declaration
public static TItem LastInSorted<TItem>(this IEnumerable<TItem> source, Func<TItem, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TItem> | source | |
| Func<TItem, Boolean> | predicate | A delegate to test each element for a condition. |
Returns
| Type | Description |
|---|---|
| TItem | The last element in a sorted collection that satisfies the |
Type Parameters
| Name | Description |
|---|---|
| TItem |
Remarks
The collection is expected to be sorted. Otherwise this method can yield unexpected results. The search will stop after the last consecutive match.
While the standard Last<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) iterates and caches the complete collection in order to produce a correct result for unsorted collections,
LastInSorted<TItem>(IEnumerable<TItem>, Func<TItem, Boolean>) expects a sorted collection to avoid iterating the complete collection and therefore to significantly improve the performance in terms of speed and memory footprint.
The performance gain is only relevant if the collection is sorted in ascending order.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
| InvalidOperationException | The The |
LastOrDefaultInSorted<TItem>(IEnumerable<TItem>, Func<TItem, Boolean>)
A non-cached version of LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) for sorted collections.
Declaration
public static TItem LastOrDefaultInSorted<TItem>(this IEnumerable<TItem> source, Func<TItem, bool> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TItem> | source | |
| Func<TItem, Boolean> | predicate | A delegate to test each element for a condition. |
Returns
| Type | Description |
|---|---|
| TItem | The last element in a sorted collection that satisfies the |
Type Parameters
| Name | Description |
|---|---|
| TItem |
Remarks
The collection is expected to be sorted. Otherwise this method can yield unexpected results. The search will stop after the last consecutive match.
While the standard LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) iterates and caches the complete collection in order to produce a correct result for unsorted collections,
LastOrDefaultInSorted<TItem>(IEnumerable<TItem>, Func<TItem, Boolean>) expects a sorted collection to avoid iterating the complete collection and therefore to significantly improve the performance in terms of speed and memory footprint.
The performance gain is only relevant if the collection is sorted in ascending order.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentNullException |
|
TakeRange<TItem>(IEnumerable<TItem>, Int32, Int32)
Returns a range of elements.
Declaration
public static IEnumerable<TItem> TakeRange<TItem>(this IEnumerable<TItem> source, int startIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TItem> | source | |
| Int32 | startIndex | The inclusive starting index of the range. |
| Int32 | count | The number of elements to take. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TItem> | An IEnumerable<T> that contains the requested range of the original |
Type Parameters
| Name | Description |
|---|---|
| TItem |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
ToDataTable<TData>(IEnumerable<TData>)
Converts a collection of objects to a DataTable, where public property names are translated to column names.
Declaration
public static DataTable ToDataTable<TData>(this IEnumerable<TData> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TData> | source | The source collection to convert to a DataTable. |
Returns
| Type | Description |
|---|---|
| DataTable | A DataTable created from |
Type Parameters
| Name | Description |
|---|---|
| TData | The type of the objects stored in the |
Remarks
The extension method creates a DataTable from the source object's public properties.
Each public property is translated to a column where the property name is the column name.
To control column naming, you can decorate the property with the DisplayNameAttribute attribute to provide an alternative column name for the decorated property.
To exclude public properties from the conversion, decorate them with the IgnoreAttribute attribute.
ToDictionary(Object)
Coverts any type to a Dictionary<TKey, TValue>, where the TKey is the member name and TValue the member's value.
Declaration
public static Dictionary<string, object> ToDictionary(this object instanceToConvert)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | instanceToConvert |
Returns
| Type | Description | ||||||
|---|---|---|---|---|---|---|---|
| Dictionary<String, Object> | A Dictionary<TKey, TValue>, where
This rules apply to the complete object graph. |
Remarks
This method recursivly traverses the complete object graph and converts every object node i.e. property value (execpt primitive types and String) to a Dictionary<TKey, TValue>.
It creates entries for all langword_csharp_public instance and class properties of this object. Each entry represents a property as key-value-pair of property name and property value.
To create a flat map of the object graph, use the ToFlatDictionary(Object)
Use the IgnoreInObjectGraphAttribute attribute to decorate properties that should be excluded.
ToFlatDictionary(Object)
Coverts any type to a flattened Dictionary<TKey, TValue>, where the TKey is the member's name and TValue the member's value.
Flattened in this context means, only the object graph's root is converted.
Declaration
public static Dictionary<string, object> ToFlatDictionary(this object instanceToConvert)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | instanceToConvert |
Returns
| Type | Description | ||||
|---|---|---|---|---|---|
| Dictionary<String, Object> | A Dictionary<TKey, TValue>, where
Since ToFlatDictionary(Object) returns a flat dictionary, the result graph has no depth.In other words, this rules apply to the root of the object graph. |
Remarks
This method only traverses the root of the object's graph and to convert it to a Dictionary<TKey, TValue>.
It creates entries for all langword_csharp_public instance and class properties of this object. Each entry represents a property as key-value-pair of property name (of type String) and property value (of type Object).
To traverse the complete object graph recursively (to convert every object node i.e. property value to a Dictionary<TKey, TValue>), use the ToDictionary(Object) method.
Use the IgnoreInObjectGraphAttribute attribute to decorate properties that should be excluded.
ToFunc<TParam>(Predicate<TParam>)
Converts a Predicate<T> to a Func<T, TResult>.
Declaration
public static Func<TParam, bool> ToFunc<TParam>(this Predicate<TParam> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| Predicate<TParam> | predicate | The predicate to convert. |
Returns
| Type | Description |
|---|---|
| Func<TParam, Boolean> | A |
Type Parameters
| Name | Description |
|---|---|
| TParam | The parameter type for the predicate. |