Class CollectionOps
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Inherited Members
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class CollectionOps
Methods
AddNamedValue(string, string)
Add a value to a name/value collection string of the form name=value&name2=value2
Declaration
public static string AddNamedValue(this string nameValueString, string addNameValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | addNameValue | Name/value to add (no search, will always be added) |
Returns
| Type | Description |
|---|---|
| string | Updated collection string |
AddNamedValue(string, string, string)
Add a value to a name/value collection string of the form name=value&name2=value2
Declaration
public static string AddNamedValue(this string nameValueString, string name, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | name | Name to add (no search, will always be added) |
| string | value | Value to add |
Returns
| Type | Description |
|---|---|
| string | Updated collection string |
AddRange<T, S>(Dictionary<T, S>, Dictionary<T, S>)
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Declaration
public static void AddRange<T, S>(this Dictionary<T, S> target, Dictionary<T, S> source)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<T, S> | target | |
| Dictionary<T, S> | source |
Type Parameters
| Name | Description |
|---|---|
| T | |
| S |
AddToArray<T>(T[], T)
Logically add an element to an array by creating a new array with one more slot, copying all the existing elements (by reference) and adding the new element at the end. This is an O(n) operation where n is the number of elements in the array
Declaration
public static T[] AddToArray<T>(T[] currentArray, T newElement)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | currentArray | Current array, can be null |
| T | newElement | New element, can be null, in which case the base array will be returned. If currentArray is also null, an array of length 0 will be returned. |
Returns
| Type | Description |
|---|---|
| T[] |
Type Parameters
| Name | Description |
|---|---|
| T | Base type for array |
AddToArray<T>(T[], T[])
Logically add an array of elements to an array by creating a new array with one more slot, copying all the existing elements (by reference) and adding the new element at the end. This is an O(n) operation where n is the number of elements in the array
Declaration
public static T[] AddToArray<T>(T[] currentArray, T[] newElements)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | currentArray | Current array, can be null |
| T[] | newElements | New elements, can be null, in which case the base array will be returned. If currentArray is also null, an array of length 0 will be returned |
Returns
| Type | Description |
|---|---|
| T[] |
Type Parameters
| Name | Description |
|---|---|
| T | Base type for array |
AddToDictionaryList<KeyType, ValueType>(Dictionary<KeyType, List<ValueType>>, KeyType, ValueType)
Add a value to a data structure that is a dictionary, where each value is a list of some type
Declaration
public static void AddToDictionaryList<KeyType, ValueType>(this Dictionary<KeyType, List<ValueType>> destination, KeyType key, ValueType val)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<KeyType, List<ValueType>> | destination | Dictionary to be updated |
| KeyType | key | Dictionary key |
| ValueType | val | Inner list value for the given key |
Type Parameters
| Name | Description |
|---|---|
| KeyType | The dictionary key type |
| ValueType | The inner value type of the list, whic is the value type of the dictionary |
Examples
Suppose we have to store a number of values for each key. A normal dictionary is unique on key, so we need to store the values in a list inside each dictionary value.
The declaration can look like this:private static Dictionary<SoTable, List<CTData>> _cachedTables;"
To add a new CTData using some SoTable type key, use the following:
SoTable key = GetSoTable( mySomething );
CTData listValue = new CTData( myDataSomething );
_cachedTables.AddToDictionaryList( key, listValue );
If the key already exists, its value list is extended with the new value. If not,
then the key is added to the dictionary and a new list containing the single new listValue is set as its value.
AddToDictionary<Key, Value>(Dictionary<Key, Value>, Key, Value)
Add an element to a dictionary, overwriting it is it already exists
Declaration
public static void AddToDictionary<Key, Value>(Dictionary<Key, Value> target, Key newKey, Value newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<Key, Value> | target | Target dictionary |
| Key | newKey | Key of element to add |
| Value | newValue | Value to add |
Type Parameters
| Name | Description |
|---|---|
| Key | Key type |
| Value | Value type |
ArraysEquivalent<T>(T[], T[])
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Declaration
public static bool ArraysEquivalent<T>(T[] left, T[] right)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | left | |
| T[] | right |
Returns
| Type | Description |
|---|---|
| bool |
Type Parameters
| Name | Description |
|---|---|
| T |
AtLeastOne<T>(IEnumerable<T>)
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Declaration
[Obsolete("This method is obsolete and will be removed in a future version.", false)]
public static IEnumerable<T> AtLeastOne<T>(this IEnumerable<T> sequence)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | sequence |
Returns
| Type | Description |
|---|---|
| IEnumerable<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
CloneArray(string[])
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Declaration
public static string[] CloneArray(string[] originalArray)
Parameters
| Type | Name | Description |
|---|---|---|
| string[] | originalArray |
Returns
| Type | Description |
|---|---|
| string[] |
CloneArray<MemberType>(MemberType[])
Clone (deep copy) an array of elements, by creating a new array and cloning each element
Declaration
public static MemberType[] CloneArray<MemberType>(MemberType[] originalArray) where MemberType : ICloneable
Parameters
| Type | Name | Description |
|---|---|---|
| MemberType[] | originalArray | Original array to be cloned. Can be null, in which case null is also returned |
Returns
| Type | Description |
|---|---|
| MemberType[] | Cloned array |
Type Parameters
| Name | Description |
|---|---|
| MemberType | Type of array member, must be ICloneable |
ConvertArray<SourceType, ReturnType>(ICollection<SourceType>, ConvertType<SourceType, ReturnType>, Predicate<SourceType>)
Conditionally Convert an array of some type to an equal-length array of another type, using a converter and a predicate on the source type
Declaration
[Obsolete("This method is obsolete and will be removed in a future version.", false)]
public static ReturnType[] ConvertArray<SourceType, ReturnType>(ICollection<SourceType> source, CollectionOps.ConvertType<SourceType, ReturnType> converter, Predicate<SourceType> condition)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<SourceType> | source | Source array |
| CollectionOps.ConvertType<SourceType, ReturnType> | converter | Converter, called for each element |
| Predicate<SourceType> | condition |
Returns
| Type | Description |
|---|---|
| ReturnType[] | Array of target type |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Source element type |
| ReturnType | Return (target) element type |
ConvertArray<SourceType, ReturnType>(SourceType[], ConvertType<SourceType, ReturnType>)
Convert an array of some type to an equal-length array of another type, using a converter
Declaration
public static ReturnType[] ConvertArray<SourceType, ReturnType>(SourceType[] source, CollectionOps.ConvertType<SourceType, ReturnType> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| SourceType[] | source | Source array |
| CollectionOps.ConvertType<SourceType, ReturnType> | converter | Converter, called for each element |
Returns
| Type | Description |
|---|---|
| ReturnType[] | Array of target type |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Source element type |
| ReturnType | Return (target) element type |
ConvertArray<SourceType, ReturnType>(SourceType[], ConvertType<SourceType, ReturnType>, Predicate<SourceType>)
Conditionally Convert an array of some type to an equal-length array of another type, using a converter and a predicate on the source type
Declaration
[Obsolete("This method is obsolete and will be removed in a future version.", false)]
public static ReturnType[] ConvertArray<SourceType, ReturnType>(SourceType[] source, CollectionOps.ConvertType<SourceType, ReturnType> converter, Predicate<SourceType> condition)
Parameters
| Type | Name | Description |
|---|---|---|
| SourceType[] | source | Source array |
| CollectionOps.ConvertType<SourceType, ReturnType> | converter | Converter, called for each element |
| Predicate<SourceType> | condition |
Returns
| Type | Description |
|---|---|
| ReturnType[] | Array of target type |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Source element type |
| ReturnType | Return (target) element type |
ConvertToArray<SourceType, ReturnType>(ICollection<SourceType>, ConvertType<SourceType, ReturnType>)
Convert an array of some type to an equal-length array of another type, using a converter
Declaration
public static ReturnType[] ConvertToArray<SourceType, ReturnType>(ICollection<SourceType> source, CollectionOps.ConvertType<SourceType, ReturnType> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<SourceType> | source | Source array |
| CollectionOps.ConvertType<SourceType, ReturnType> | converter | Converter, called for each element |
Returns
| Type | Description |
|---|---|
| ReturnType[] | Array of target type |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Source element type |
| ReturnType | Return (target) element type |
ConvertToStringArray<SourceType>(ICollection<SourceType>)
Convert a collection of source elements into an array of string, using each element's ToString() method. A null input will result in a null output. An empty input will give an empty output (not null).
Declaration
[Obsolete("This method is obsolete and will be removed in a future version.", false)]
public static string[] ConvertToStringArray<SourceType>(ICollection<SourceType> sourceCollection)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<SourceType> | sourceCollection | Collection to be converted, can be null or empty |
Returns
| Type | Description |
|---|---|
| string[] | Array with same number of elements as input collection; or null if input was null |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Type of source element, deduced |
ConvertToStringArray<SourceType>(SourceType[])
Convert an array of source elements into an array of string, using each element's ToString() method. A null input will result in a null output. An empty input will give an empty output (not null).
Declaration
[Obsolete("This method is obsolete and will be removed in a future version.", false)]
public static string[] ConvertToStringArray<SourceType>(SourceType[] sourceArray)
Parameters
| Type | Name | Description |
|---|---|---|
| SourceType[] | sourceArray | Array to be converted, can be null or empty |
Returns
| Type | Description |
|---|---|
| string[] | Array with same number of elements as input collection; or null if input was null |
Type Parameters
| Name | Description |
|---|---|
| SourceType | Type of source element, deduced |
CreateDictionaryFromArray<KeyType, ValueType>(ValueType[], GetKeyFromValueItem<KeyType, ValueType>)
Create a dictionary of key, item pairs from an array if items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromArray<KeyType, ValueType>(ValueType[] array, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueType[] | array | Array if items to create the dictionary from |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromArray<KeyType, ValueType>(ValueType[], GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>)
Create a dictionary of key, item pairs from an array of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromArray<KeyType, ValueType>(ValueType[] array, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueType[] | array | Array if items to create the dictionary from |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | delegate that extracts the key from the item, see GetKeyFromValueItem |
| IEqualityComparer<KeyType> | comparer | Comparer to use for the keys, an example would be OrdinalIgnoreCase if the key type were string and you needed case-insensitive comparisons |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromArray<KeyType, ValueType>(ValueType[], GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from an array if items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromArray<KeyType, ValueType>(ValueType[] array, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueType[] | array | Array if items to create the dictionary from |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| IEqualityComparer<KeyType> | comparer | |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromArray<KeyType, ValueType>(ValueType[], GetKeyFromValueItem<KeyType, ValueType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from an array if items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromArray<KeyType, ValueType>(ValueType[] array, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueType[] | array | Array if items to create the dictionary from |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType>, GetKeyFromValueItem<KeyType, ValueType>)
Create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<ValueType> | collection | Array if items to create the dictionary from |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>)
Create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| IEqualityComparer<KeyType> | comparer |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| IEqualityComparer<KeyType> | comparer | |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromCollection<KeyType, ValueType>(ICollection<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType>, GetKeyFromValueItem<KeyType, ValueType>)
Create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>)
Create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| IEqualityComparer<KeyType> | comparer |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, IEqualityComparer<KeyType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, IEqualityComparer<KeyType> comparer, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| IEqualityComparer<KeyType> | comparer | |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType>, GetKeyFromValueItem<KeyType, ValueType>, Predicate<ValueType>)
Conditionally create a dictionary of key, item pairs from a collection of items and a method that extract the key information
Declaration
[Obsolete("This method is obsolete and will be removed in a future version. Use IEnumerableExtensions.ToDictionaryPermissive(...) instead.", false)]
public static Dictionary<KeyType, ValueType> CreateDictionaryFromEnumerable<KeyType, ValueType>(IEnumerable<ValueType> collection, CollectionOps.GetKeyFromValueItem<KeyType, ValueType> keyExtractor, Predicate<ValueType> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<ValueType> | collection | |
| CollectionOps.GetKeyFromValueItem<KeyType, ValueType> | keyExtractor | Delegate that extracts the key from the item, see GetKeyFromValueItem. This delegate takes one parameter of type ValueType and returns a KeyType |
| Predicate<ValueType> | predicate | Predicate, evaluated for each item. The item is added to the dictionary if the predicate returns true |
Returns
| Type | Description |
|---|---|
| Dictionary<KeyType, ValueType> | Dictionary, populated with the keys and corresponding items |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Type of key |
| ValueType | Type of value |
DeepCloneArray<T>(T[])
Clone an array, by cloning each element of it
Declaration
public static T[] DeepCloneArray<T>(T[] input) where T : ICloneable
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | input | Array to be cloned, null and empty are ok |
Returns
| Type | Description |
|---|---|
| T[] | Deep clone; empty array if input is null or empty |
Type Parameters
| Name | Description |
|---|---|
| T |
DictionaryKeysToArray<KeyType, ValueType>(Dictionary<KeyType, ValueType>)
Extract the key collection from a dictionary, and return it as an array of the dictionary key type
Declaration
public static KeyType[] DictionaryKeysToArray<KeyType, ValueType>(Dictionary<KeyType, ValueType> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<KeyType, ValueType> | dictionary | Dictionary to extract keys from; if null, an empty array will be returned |
Returns
| Type | Description |
|---|---|
| KeyType[] | Dictionary keys as an array |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Dictionary key type |
| ValueType | Dictionary value type |
Remarks
Type inference means that you do not have to explicitly specify the types involved,
simply pass in a dictionary and the generic code will adapt.
Dictionary<string, MyType> dictionary = new Dictionary<string, MyType>();
// add some members somehow
string[] myKeys = SuperOffice.Util.DictionaryKeysToArray( dictionary );
DictionaryValuesToArray<KeyType, ValueType>(Dictionary<KeyType, ValueType>)
Extract the value collection from a dictionary, and return it as an array of the dictionary value type
Declaration
public static ValueType[] DictionaryValuesToArray<KeyType, ValueType>(Dictionary<KeyType, ValueType> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<KeyType, ValueType> | dictionary | Dictionary to extract values from; if null, an empty array will be returned |
Returns
| Type | Description |
|---|---|
| ValueType[] | Dictionary values as an array |
Type Parameters
| Name | Description |
|---|---|
| KeyType | Dictionary key type |
| ValueType | Dictionary value type |
Remarks
Type inference means that you do not have to explicitly specify the types involved,
simply pass in a dictionary and the generic code will adapt.
Dictionary<string, MyType> dictionary = new Dictionary<string, MyType>();
// add some members somehow
MyType[] myValues = SuperOffice.Util.DictionaryValuesToArray( dictionary );
GetFirstElement<K, V>(Dictionary<K, V>)
Return the first element in a dictionary
Declaration
public static KeyValuePair<K, V> GetFirstElement<K, V>(Dictionary<K, V> source)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<K, V> | source | Dictionary to get the first element from |
Returns
| Type | Description |
|---|---|
| KeyValuePair<K, V> | First element |
Type Parameters
| Name | Description |
|---|---|
| K | Dictionary key type |
| V | Dictionary value type |
GetNamedBoolValue(string, string)
Find and parse a bool-typed option in a named-value string, and return the result
Declaration
public static bool GetNamedBoolValue(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| bool | true if the option value is 1 or (case-insentive) 'true' |
GetNamedDateTimeValue(string, string)
Find and parse a DateTime-typed option in a named-value string, and return the result
Declaration
public static DateTime GetNamedDateTimeValue(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| DateTime | DateTime value, or DateTime.MinValue if option not found or invalid format |
GetNamedDoubleValue(string, string)
Find and parse a double-typed option in a named-value string, and return the result
Declaration
public static double GetNamedDoubleValue(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| double | double value, or 0.0 if option not found or invalid format |
GetNamedIntValue(string, string, int)
Find and parse a int-typed option in a named-value string, and return the result
Declaration
public static int GetNamedIntValue(this string nameValueString, string nameToFind, int defaultValue = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
| int | defaultValue |
Returns
| Type | Description |
|---|---|
| int | Integer value, or 0 if option not found or invalid format |
GetNamedIntValues(string, string)
Find and parse a int array typed option in a named-value string, and return the result
Declaration
public static List<int> GetNamedIntValues(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| List<int> | Integer values, or 0 if option not found or invalid format |
GetNamedValue(string, string)
Return a value from a name/value collection string of the form name=value&name2=value2
Declaration
public static string GetNamedValue(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | nameToFind | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| string | The value of the key, or string.Empty if not found |
GetPairs(string)
Parse a string that contains one or more key=value pairs into a list of KeyValuePair<string,string>. NOTE that key names are lower-cased during parsing!
Declaration
public static List<KeyValuePair<string, string>> GetPairs(string keyValueString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | keyValueString | String of the form key=value, with & as separator between multiple pairs |
Returns
| Type | Description |
|---|---|
| List<KeyValuePair<string, string>> | List of KeyValuePair elements, in the same order as they were in the string |
HasName(string, string)
Check if a named-value string (querystring) contains a given name; null-tolerant and case-insensitive like all such operations
Declaration
public static bool HasName(this string nameValueString, string nameToFind)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | |
| string | nameToFind |
Returns
| Type | Description |
|---|---|
| bool |
HasName(string, string, bool)
Utility class for operations on collections of various kinds. You will find operations for converting between various kinds of collections, converting the data inside collections, comparisons and more
Declaration
public static bool HasName(this string nameValueString, string nameToFind, bool requireValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | |
| string | nameToFind | |
| bool | requireValue |
Returns
| Type | Description |
|---|---|
| bool |
IsNullOrEmpty<BaseType>(ICollection<BaseType>)
Check if a given collection/array is null, or contains no elements. Semantically equivalent to string.IsNullOrEmpty(string).
Declaration
public static bool IsNullOrEmpty<BaseType>(ICollection<BaseType> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| ICollection<BaseType> | collection | Array, or null |
Returns
| Type | Description |
|---|---|
| bool | True if the in-parameter is null or has no elements |
Type Parameters
| Name | Description |
|---|---|
| BaseType | Base type for array |
IsNullOrEmpty<BaseType>(BaseType[])
Check if a given collection/array is null, or contains no elements. Semantically equivalent to string.IsNullOrEmpty(string).
Declaration
public static bool IsNullOrEmpty<BaseType>(BaseType[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| BaseType[] | array | Array, or null |
Returns
| Type | Description |
|---|---|
| bool | True if the in-parameter is null or has no elements |
Type Parameters
| Name | Description |
|---|---|
| BaseType | Base type for array |
NullToEmpty(string)
Convert a null string into an empty string
Declaration
public static string NullToEmpty(string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | Input string, which might be null |
Returns
| Type | Description |
|---|---|
| string | Original non-null string, or the empty string |
NullToEmpty<T>(T[])
Convert a null pointer into an empty array
Declaration
public static T[] NullToEmpty<T>(T[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | Actual array, which might be null |
Returns
| Type | Description |
|---|---|
| T[] | Either the original non-emtpy array, or an empty array |
Type Parameters
| Name | Description |
|---|---|
| T | Type we have an array of |
ParseDelimitedStringToIntArray(string)
Parse a delimited string like '1, 3,4,56, 23 15' to the int[]{1, 3, 4, 56, 23, 15}. This overload uses space, comma and semicolon as delimiters.
Declaration
public static List<int> ParseDelimitedStringToIntArray(string delimitedString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | delimitedString | Input string |
Returns
| Type | Description |
|---|---|
| List<int> | Int array of parseable values |
Remarks
This method will split apart the input string according to the delimiters, treating multiple consecutive delimiters as one. Each substring will be parsed using int.TryParse(string, NumberStyles, IFormatProvider, out int), and the result placed in the return list. If TryParse fails, no value is parsed (no exceptions thrown). An empty input will yield an empty (Length == 0) result.
ParseDelimitedStringToIntArray(string, params char[])
Parse a delimited string like '1, 3,4,56, 23 15' to the int[]{1, 3, 4, 56, 23, 15}
Declaration
public static List<int> ParseDelimitedStringToIntArray(string delimitedString, params char[] delimiters)
Parameters
| Type | Name | Description |
|---|---|---|
| string | delimitedString | Input string |
| char[] | delimiters | Array of delimiter characters; multiple consecutive delimiters will count as one |
Returns
| Type | Description |
|---|---|
| List<int> | Int array of parseable values |
Remarks
This method will split apart the input string according to the delimiters, treating multiple consecutive delimiters as one. Each substring will be parsed using int.TryParse(string, NumberStyles, IFormatProvider, out int), and the result placed in the return list. If TryParse fails, no value is parsed (no exceptions thrown). An empty input will yield an empty (Length == 0) result.
RemoveNamedValue(string, string)
Remove values in a name/value collection string of the form name=value&name2=value2. All occurances of the key will be removed.
Declaration
public static string RemoveNamedValue(this string nameValueString, string keyName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | keyName | Name of key to find (case insensitive) |
Returns
| Type | Description |
|---|---|
| string |
ReplaceNamedValue(string, string, string)
Replace a value in a name/value collection string of the form name=value&name2=value2
Declaration
public static string ReplaceNamedValue(this string nameValueString, string keyName, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection string; empty and null are legal |
| string | keyName | Name of key to find (case insensitive) |
| string | value | New value to set |
Returns
| Type | Description |
|---|---|
| string | Updated collection string |
SortNamedValuesByNames(string)
Sort a name/value collection string of the form name=value&name2=value2 by key names alphabetically ascending
Declaration
public static string SortNamedValuesByNames(this string nameValueString)
Parameters
| Type | Name | Description |
|---|---|---|
| string | nameValueString | Name/value collection to sort |
Returns
| Type | Description |
|---|---|
| string | Name/values is sorted order |
StringArrayToStringDictionary(string[])
Convert an array of key=value pairs into a StringDictionary, using the first equals sign in each string as the key/value delimiter
Declaration
public static Dictionary<string, string> StringArrayToStringDictionary(string[] input)
Parameters
| Type | Name | Description |
|---|---|---|
| string[] | input | String array, null and empty are both legal |
Returns
| Type | Description |
|---|---|
| Dictionary<string, string> | StringDictionary based on input array. Duplicate keys are skipped and their values are lost |
StringDictionaryToStringArray(Dictionary<string, string>)
Convert a StringDictionary into an array of key=value strings
Declaration
public static string[] StringDictionaryToStringArray(Dictionary<string, string> input)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, string> | input | Input dictionary, sincerely hoping none of the keys contain an equals sign. null and empty are both legal |
Returns
| Type | Description |
|---|---|
| string[] | Array of key=value pairs, each a string. Empty array if input was null or empty |