Show / Hide Table of Contents

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

Inheritance
Object
CollectionOps
Inherited Members
Object.ToString()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
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>)

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

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

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.

ArraysEquivalent<T>(T[], T[])

Check if two arrays have the same contents. The arrays must be of the same type, and the base type must be comparable so that it can be sorted. The algorithm is O(log n) if the arrays have the same size, otherwise it is very fast.

Declaration
public static bool ArraysEquivalent<T>(T[] left, T[] right)
Parameters
Type Name Description
T[] left

Left side of comparison; if null, a false result will always be returned

T[] right

Right side of comparison; if null, a false result will always be returned

Returns
Type Description
Boolean

True if the arrays have the same length and same elements, regardless of element order

Type Parameters
Name Description
T

Base type of arrays

Remarks

Type inference means that you do not have to explicitly specify the actual type, simply pass in two arrays. This example shows how:

int[] allAssocs = AssociateCache.GetCurrent().GetAssociateIds();
int[] myAssocs = { 1, 2, 3 };
bool areMyAllThereAre = SuperOffice.Util.ArraysEquivalent( allAssocs, myAssocs );

AtLeastOne<T>(IEnumerable<T>)

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

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>(SourceType[], CollectionOps.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[], CollectionOps.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
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

ConvertArray<SourceType, ReturnType>(ICollection<SourceType>, CollectionOps.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
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

ConvertToArray<SourceType, ReturnType>(ICollection<SourceType>, CollectionOps.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>(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
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

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

CreateDictionaryFromArray<KeyType, ValueType>(ValueType[], CollectionOps.GetKeyFromValueItem<KeyType, ValueType>)

Create a dictionary of key, item pairs from an array if items and a method that extract the key information

Declaration
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 . This delegate takes one parameter of type ValueType and returns a

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[], CollectionOps.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
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

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[], CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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[], CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.GetKeyFromValueItem<KeyType, ValueType>)

Create a dictionary of key, item pairs from a collection of items and a method that extract the key information

Declaration
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.GetKeyFromValueItem<KeyType, ValueType>)

Create a dictionary of key, item pairs from a collection of items and a method that extract the key information

Declaration
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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>, CollectionOps.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
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 . This delegate takes one parameter of type ValueType and returns a

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. This is how it works:

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. This is how it works:

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
Boolean

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)

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

Integer value, 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
Boolean

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
Boolean

True if the in-parameter is null or has no elements

Type Parameters
Name Description
BaseType

Base type for array

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
Boolean

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<Int32>

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, Int32), 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, 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<Int32>

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, Int32), 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 , 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 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

© SuperOffice. All rights reserved.
SuperOffice |  Community |  Release Notes |  Privacy |  Site feedback |  Search Docs |  About Docs |  Contribute |  Back to top