65.9K
CodeProject is changing. Read more.
Home

The Elegant Art of Programming

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

May 16, 2012

CPOL
viewsIcon

26326

This is an alternative for "The Elegant Art of Programming"

This is an alternative to the original poster's "solution 1", which really does need some help.

    public static bool
    AllTheSame<T>
    (                                 
      this System.Collections.Generic.IList<T> List
    )                                      
    where T : System.IEquatable<T>
    {                                                  
      if ( List == null )
      {
        throw ( new System.ArgumentNullException ( "List" , "List must not be null" ) ) ;
      }

      bool result = true ;

      if ( List.Count > 1 )
      {                                
        T first = List [ 0 ] ;    
                                
        if ( (object) first == null )
        {
          for ( int i = 1 ; result && ( i < List.Count ) ; i++ )
          {
            result = (object) List [ i ] == null ;
          }
        }
        else
        {
          for ( int i = 1 ; result && ( i < List.Count ) ; i++ )
          {                         
            result = first.Equals ( List [ i ] ) ;
          }
        }
      }
    
      return ( result ) ;
    }