Search Results for

    Show / Hide Table of Contents

    Class HelperExtensionsCommon

    A collection of extension methods for various default types

    Inheritance
    Object
    HelperExtensionsCommon
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Object.ReferenceEquals(Object, Object)
    Object.ToString()
    Namespace: BionicCode.Utilities.Net
    Assembly: BionicCode.Utilities.Net.Common.dll
    Syntax
    public static class HelperExtensionsCommon

    Methods

    | Improve this Doc View Source

    AddColumn<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[])

    | Improve this Doc View Source

    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 of TData for all existing rows.
    AddColumn<TData>(DataTable, String, Int32) should be called before an overload of AddRow(DataTable, Object[])
    | Improve this Doc View Source

    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

    source is null.

    ArgumentNullException

    range parameter is null.

    | Improve this Doc View Source

    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

    source is null.

    ArgumentNullException

    range parameter is null.

    | Improve this Doc View Source

    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 IEnumerable<KeyValuePair<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

    source is null.

    ArgumentNullException

    range parameter is null.

    | Improve this Doc View Source

    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 IEnumerable<(TKey Key,TValue Value>> 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

    source is null.

    ArgumentNullException

    range parameter is null.

    | Improve this Doc View Source

    AddRow(DataTable, IEnumerable<Object>)

    Adds a new DataRow to a DataTable.

    Declaration
    public static void AddRow(this DataTable source, IEnumerable<object> columnValues)
    Parameters
    Type Name Description
    DataTable source

    The DataTable to add the new DataRow to.

    IEnumerable<Object> columnValues

    The ordered row's cell values, where the item index maps to the column index.

    Exceptions
    Type Condition
    IndexOutOfRangeException

    When columnValues contains less elements than source has columns.

    | Improve this Doc View Source

    AddRow(DataTable, Object[])

    Adds a new DataRow to a DataTable.

    Declaration
    public static void AddRow(this DataTable source, params object[] columnValues)
    Parameters
    Type Name Description
    DataTable source

    The DataTable to add the new DataRow to.

    Object[] columnValues

    The row's cell values ordered by column index.

    | Improve this Doc View Source

    AddRow(DataTable, (Int32 ColumnIndex, Object ColumnValue)[])

    Adds a new DataRow to a DataTable.

    Declaration
    public static void AddRow(this DataTable source, params (int ColumnIndex, object ColumnValue)[] columnValues)
    Parameters
    Type Name Description
    DataTable source

    The DataTable to add the new DataRow to.

    System.ValueTuple<Int32, Object>[] columnValues

    The row's cell values passed as tuples of (int ColumnIndex, object ColumnValue).

    Exceptions
    Type Condition
    IndexOutOfRangeException

    When columnValues contains less elements than source has columns or a contained tuple has an invalid index.

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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 source is empty. Otherwise false

    Type Parameters
    Name Description
    TItem
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Improve this Doc View Source

    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 predicate delegate. If no such an element was found, a InvalidOperationException exception will be thrown.

    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

    source is null.

    ArgumentNullException

    predicate is null.

    InvalidOperationException

    The source collection does not contain any element that satisfies the predicate delegate.

    The source collection is empty.

    | Improve this Doc View Source

    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 predicate delegate or null if no such element was found.

    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

    source is null.

    ArgumentNullException

    predicate is null.

    | Improve this Doc View Source

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

    Type Parameters
    Name Description
    TItem
    Exceptions
    Type Condition
    ArgumentNullException

    source is null.

    | Improve this Doc View Source

    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 public properties of the objects contained in the source.

    Type Parameters
    Name Description
    TData

    The type of the objects stored in the source collection.

    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.

    | Improve this Doc View Source

    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

    TKey TValue
    is the member's name of type String is the member's value of type Object, when the value is a primitive type like langword_csharp_int or a String
    is the member's value of type Dictionary<TKey, TValue>, when the value is a complex type that itself was converted to a Dictionary<string, object>
    .
    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.

    | Improve this Doc View Source

    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

    TKey TValue
    is the member's name of type String is the member's value of type Object

    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.

    | Improve this Doc View Source

    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 FuncTParam, bool> that returns the result of predicate.

    Type Parameters
    Name Description
    TParam

    The parameter type for the predicate.

    • Improve this Doc
    • View Source
    In This Article
    Back to top Generated by DocFX