Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

The Elegant Art of Programming

5.00/5 (1 vote)
18 May 2012CPOL 26.2K  
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.

C#
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 ) ;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)