 Stumble
Stumble
 
 
 del.icio.us
del.icio.us
 Reddit
Reddit
 MyWeb!
MyWeb!
 Facebook
Facebook
 Google bookmark
Google bookmark
It's been a while since I wrote here. Anyways... Most of us developers have come across multiple such sitations where we wanted a data structure which could contain two different data types which could then be returned to a function call.     1 // Copyright © 2009 Yours truly.  All rights reserved.     2      3 using System;     4      5 namespace MyIniFileReader     6 {     7     //**************************************************************************     8     /// <summary>     9     ///    An immutable data structure to store any two arbitrary objects.    10     /// </summary>    11     /// <remarks>    12     /// <para>This struct can be used as the return type for any method that    13     /// logically needs to return two values. Or, it can be used to supply two    14     /// values to a method that is defined to accept only one parameter.</para>    15     /// <para>This class is based upon the article <i>Generics: Move Beyond    16     /// Collections</i> by Bill Wagner in the April 2007 edition of Visual    17     /// Studio Magazine.</para>    18     /// <para>$Id$</para>    19     /// <author>Author: Yours truly</author>    20     /// </remarks>    21     /// <typeparam name="T1">The type of the first member.</typeparam>    22     /// <typeparam name="T2">The type of the second member.</typeparam>    23     //**************************************************************************    24     [Serializable]    25     public struct MyTuple<T1, T2> : IEquatable<MyTuple<T1, T2>>    26     {    27         private readonly T1 m_first;    28         private readonly T2 m_second;    29     30         //**********************************************************************    31         /// <summary>    32         /// Initializes a new instance of the <see cref="MyTuple{T1,T2}"/>    33         /// structure.    34         /// </summary>    35         /// <param name="first">The first member.</param>    36         /// <param name="second">The second member.</param>    37         public MyTuple(T1 first, T2 second)    38         {    39             m_first = first;    40             m_second = second;    41         }    42     43         //**********************************************************************    44         /// <summary>    45         /// Gets the first value.    46         /// </summary>    47         /// <value>An entity of type <typeparamref name="T1"/>.</value>    48         public T1 First    49         {    50             get { return (m_first); }    51         }    52     53         //**********************************************************************    54         /// <summary>    55         /// Gets the second value.    56         /// </summary>    57         /// <value>An entity of type <typeparamref name="T2"/>.</value>    58         public T2 Second    59         {    60             get { return (m_second); }    61         }    62     63         #region IEquatable<MyTuple<T1,T2>> implementation    64     65         //**********************************************************************    66         /// <summary>    67         /// Determines whether the specified object is equal to the current    68         /// <see cref="MyTuple{T1,T2}"/>.    69         /// </summary>    70         /// <param name="obj">The object to compare with the current <c>MyTuple</c>.</param>    71         /// <returns><see langword="true"/> if the specified object is equal to    72         /// the current <c>MyTuple</c>; otherwise, <see langword="false"/>.</returns>    73         public override bool Equals(object obj)    74         {    75             if (obj is MyTuple<T1, T2>)    76             {    77                 return (Equals((MyTuple<T1, T2>) obj));    78             }    79             return (false);    80         } // end Equals (object)    81     82         //**********************************************************************    83         /// <summary>    84         /// Determines whether the specified <see cref="MyTuple{T1,T2}"/> is    85         /// equal to the current <c>MyTuple</c>.    86         /// </summary>    87         /// <param name="tuple">The <c>MyTuple</c> to compare with the current    88         /// <c>MyTuple</c>.</param>    89         /// <returns><see langword="true"/> if the specified <c>MyTuple</c> is    90         /// equal to the current <c>MyTuple</c>; otherwise,    91         /// <see langword="false"/>.</returns>    92         public bool Equals(MyTuple<T1, T2> tuple)    93         {    94             return (First.Equals(tuple.First) && Second.Equals(tuple.Second));    95         } // end Equals (MyTuple<T1,T2>)    96     97         //**********************************************************************    98         /// <summary>    99         /// Serves as a hash function for a particular type, suitable for use in   100         /// hashing algorithms and data structures like a hash table.   101         /// </summary>   102         /// <returns>A hash code for the current <c>MyTuple</c>.</returns>   103         public override int GetHashCode()   104         {   105             return (First.GetHashCode() ^ Second.GetHashCode());   106         }   107    108         //**********************************************************************   109         /// <summary>   110         /// Returns a boolean value that indicates whether two   111         /// <see cref="MyTuple{T1,T2}"/> values are equal.   112         /// </summary>   113         /// <param name="tuple1">An <c>MyTuple</c> instance.</param>   114         /// <param name="tuple2">An <c>MyTuple</c> instance.</param>   115         /// <returns><see langword="true"/> if the values of <paramref name="tuple1"/>   116         ///        and <paramref name="tuple2"/> are equal; otherwise,   117         ///        <see langword="false"/>.</returns>   118         /// <seealso cref="Equals (MyTuple{T1,T2})"/>   119         public static bool operator ==(MyTuple<T1, T2> tuple1,   120                                         MyTuple<T1, T2> tuple2)   121         {   122             return tuple1.Equals(tuple2);   123         }   124    125         //**********************************************************************   126         /// <summary>   127         /// Returns a boolean value that indicates whether two   128         /// <see cref="MyTuple{T1,T2}"/> values are not equal.   129         /// </summary>   130         /// <param name="tuple1">An <c>MyTuple</c> instance.</param>   131         /// <param name="tuple2">An <c>MyTuple</c> instance.</param>   132         /// <returns><see langword="true"/> if the values of <paramref name="tuple1"/>   133         ///        and <paramref name="tuple2"/> are <b>not</b> equal; otherwise,   134         ///        <see langword="false"/>.</returns>   135         /// <seealso cref="Equals (MyTuple{T1,T2})"/>   136         public static bool operator !=(MyTuple<T1, T2> tuple1,   137                                         MyTuple<T1, T2> tuple2)   138         {   139             return !tuple1.Equals(tuple2);   140         }   141    142         #endregion IEquatable<MyTuple<T1,T2>> implementation   143    144     } // end struct MyTuple   145    146 } // end namespace MyIniFileReader
Here is a Tuple class which does exactly the same ....
Hope this helps someone...
 
 Subscribe to My Codes
 Subscribe to My Codes

0 comments:
Post a Comment
Please leave your opinion...