Struct Pair<TFirst, TSecond>
Stores a pair of objects within a single struct. This struct is useful to use as the T of a collection, or as the TKey or TValue of a dictionary.
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
[Serializable]
public struct Pair<TFirst, TSecond> : IComparable, IComparable<Pair<TFirst, TSecond>>
Type Parameters
| Name | Description |
|---|---|
| TFirst | |
| TSecond |
Constructors
| Improve this Doc View SourcePair(TFirst, TSecond)
Creates a new pair with given first and second elements.
Declaration
public Pair(TFirst first, TSecond second)
Parameters
| Type | Name | Description |
|---|---|---|
| TFirst | first | The first element of the pair. |
| TSecond | second | The second element of the pair. |
Pair(KeyValuePair<TFirst, TSecond>)
Creates a new pair using elements from a KeyValuePair structure. The First element gets the Key, and the Second elements gets the Value.
Declaration
public Pair(KeyValuePair<TFirst, TSecond> keyAndValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.KeyValuePair<TFirst, TSecond> | keyAndValue | The KeyValuePair to initialize the Pair with . |
Fields
| Improve this Doc View SourceFirst
The first element of the pair.
Declaration
public TFirst First
Field Value
| Type | Description |
|---|---|
| TFirst |
Second
The second element of the pair.
Declaration
public TSecond Second
Field Value
| Type | Description |
|---|---|
| TSecond |
Methods
| Improve this Doc View SourceCompareTo(Pair<TFirst, TSecond>)
Compares this pair to another pair of the some type. The pairs are compared by using the IComparable<T> or IComparable interface on TFirst and TSecond. The pairs are compared by their first elements first, if their first elements are equal, then they are compared by their second elements.
If either TFirst or TSecond does not implement IComparable<T> or IComparable, then an NotSupportedException is thrown, because the pairs cannot be compared.
Declaration
public int CompareTo(Pair<TFirst, TSecond> other)
Parameters
| Type | Name | Description |
|---|---|---|
| Pair<TFirst, TSecond> | other | The pair to compare to. |
Returns
| Type | Description |
|---|---|
| System.Int32 | An integer indicating how this pair compares to |
Exceptions
| Type | Condition |
|---|---|
| System.NotSupportedException | Either FirstSecond or TSecond is not comparable via the IComparable<T> or IComparable interfaces. |
Equals(Object)
Determines if this pair is equal to another object. The pair is equal to another object if that object is a Pair, both element types are the same, and the first and second elements both compare equal using object.Equals.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | obj | Object to compare for equality. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the objects are equal. False if the objects are not equal. |
Overrides
Equals(Pair<TFirst, TSecond>)
Determines if this pair is equal to another pair. The pair is equal if the first and second elements both compare equal using IComparable<T>.Equals or object.Equals.
Declaration
public bool Equals(Pair<TFirst, TSecond> other)
Parameters
| Type | Name | Description |
|---|---|---|
| Pair<TFirst, TSecond> | other | Pair to compare with for equality. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the pairs are equal. False if the pairs are not equal. |
GetHashCode()
Returns a hash code for the pair, suitable for use in a hash-table or other hashed collection. Two pairs that compare equal (using Equals) will have the same hash code. The hash code for the pair is derived by combining the hash codes for each of the two elements of the pair.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| System.Int32 | The hash code. |
Overrides
ToKeyValuePair()
Converts this Pair to a KeyValuePair. The Key part of the KeyValuePair gets the First element, and the Value part of the KeyValuePair gets the Second elements.
Declaration
public KeyValuePair<TFirst, TSecond> ToKeyValuePair()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.KeyValuePair<TFirst, TSecond> | The KeyValuePair created from this Pair. |
ToString()
Returns a string representation of the pair. The string representation of the pair is
of the form:
First: {0}, Second: {1}
where {0} is the result of First.ToString(), and {1} is the result of Second.ToString() (or
"null" if they are null.)
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | The string representation of the pair. |
Overrides
Operators
| Improve this Doc View SourceEquality(Pair<TFirst, TSecond>, Pair<TFirst, TSecond>)
Determines if two pairs are equal. Two pairs are equal if the first and second elements both compare equal using IComparable<T>.Equals or object.Equals.
Declaration
public static bool operator ==(Pair<TFirst, TSecond> pair1, Pair<TFirst, TSecond> pair2)
Parameters
| Type | Name | Description |
|---|---|---|
| Pair<TFirst, TSecond> | pair1 | First pair to compare. |
| Pair<TFirst, TSecond> | pair2 | Second pair to compare. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the pairs are equal. False if the pairs are not equal. |
Explicit(KeyValuePair<TFirst, TSecond> to Pair<TFirst, TSecond>)
Converts a KeyValuePair structure into a Pair. The First element gets the Key, and the Second element gets the Value.
Declaration
public static explicit operator Pair<TFirst, TSecond>(KeyValuePair<TFirst, TSecond> keyAndValue)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.KeyValuePair<TFirst, TSecond> | keyAndValue | The KeyValuePair to convert. |
Returns
| Type | Description |
|---|---|
| Pair<TFirst, TSecond> | The Pair created by converted the KeyValuePair into a Pair. |
Explicit(Pair<TFirst, TSecond> to KeyValuePair<TFirst, TSecond>)
Converts a Pair to a KeyValuePair. The Key part of the KeyValuePair gets the First element, and the Value part of the KeyValuePair gets the Second elements.
Declaration
public static explicit operator KeyValuePair<TFirst, TSecond>(Pair<TFirst, TSecond> pair)
Parameters
| Type | Name | Description |
|---|---|---|
| Pair<TFirst, TSecond> | pair | Pair to convert. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.KeyValuePair<TFirst, TSecond> | The KeyValuePair created from |
Inequality(Pair<TFirst, TSecond>, Pair<TFirst, TSecond>)
Determines if two pairs are not equal. Two pairs are equal if the first and second elements both compare equal using IComparable<T>.Equals or object.Equals.
Declaration
public static bool operator !=(Pair<TFirst, TSecond> pair1, Pair<TFirst, TSecond> pair2)
Parameters
| Type | Name | Description |
|---|---|---|
| Pair<TFirst, TSecond> | pair1 | First pair to compare. |
| Pair<TFirst, TSecond> | pair2 | Second pair to compare. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the pairs are not equal. False if the pairs are equal. |
Explicit Interface Implementations
| Improve this Doc View SourceIComparable.CompareTo(Object)
Compares this pair to another pair of the some type. The pairs are compared by using the IComparable<T> or IComparable interface on TFirst and TSecond. The pairs are compared by their first elements first, if their first elements are equal, then they are compared by their second elements.
If either TFirst or TSecond does not implement IComparable<T> or IComparable, then an NotSupportedException is thrown, because the pairs cannot be compared.
Declaration
int IComparable.CompareTo(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | obj | The pair to compare to. |
Returns
| Type | Description |
|---|---|
| System.Int32 | An integer indicating how this pair compares to |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
| System.NotSupportedException | Either FirstSecond or TSecond is not comparable via the IComparable<T> or IComparable interfaces. |