Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / C#
Tip/Trick

The Elegant Art of Programming

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 May 2012CPOL 24.5K   3   23
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)


Written By
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian

---------------

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

 
SuggestionHow about a linq'ish approach? Pin
Andreas Gieriet18-May-12 4:46
professionalAndreas Gieriet18-May-12 4:46 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult18-May-12 5:31
mvePIEBALDconsult18-May-12 5:31 
AnswerRe: How about a linq'ish approach? Pin
Luc Pattyn18-May-12 6:27
sitebuilderLuc Pattyn18-May-12 6:27 
GeneralRe: How about a linq'ish approach? Pin
Andreas Gieriet18-May-12 21:14
professionalAndreas Gieriet18-May-12 21:14 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult19-May-12 5:01
mvePIEBALDconsult19-May-12 5:01 
AnswerRe: How about a linq'ish approach? Pin
Luc Pattyn19-May-12 5:17
sitebuilderLuc Pattyn19-May-12 5:17 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult18-May-12 7:38
mvePIEBALDconsult18-May-12 7:38 
GeneralRe: How about a linq'ish approach? Pin
Andreas Gieriet19-May-12 2:41
professionalAndreas Gieriet19-May-12 2:41 
GeneralRe: How about a linq'ish approach? Pin
PIEBALDconsult19-May-12 5:20
mvePIEBALDconsult19-May-12 5:20 
Questiona problem Pin
Luc Pattyn17-May-12 14:44
sitebuilderLuc Pattyn17-May-12 14:44 
AnswerRe: a problem Pin
PIEBALDconsult17-May-12 16:58
mvePIEBALDconsult17-May-12 16:58 
GeneralRe: a problem Pin
Luc Pattyn17-May-12 17:14
sitebuilderLuc Pattyn17-May-12 17:14 
GeneralRe: a problem Pin
PIEBALDconsult17-May-12 17:34
mvePIEBALDconsult17-May-12 17:34 
GeneralRe: a problem Pin
PIEBALDconsult18-May-12 6:20
mvePIEBALDconsult18-May-12 6:20 
AnswerRe: a problem Pin
Luc Pattyn18-May-12 6:30
sitebuilderLuc Pattyn18-May-12 6:30 
Yes, that is one of the things I suggested.

In the mean time Andreas has come up with an approach I'd call definitive. BTW: Him mentioning LINQ seems to have confused you, as his code isn't LINQ at all.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: a problem Pin
PIEBALDconsult18-May-12 7:16
mvePIEBALDconsult18-May-12 7:16 
Questionargh Pin
Luc Pattyn17-May-12 3:56
sitebuilderLuc Pattyn17-May-12 3:56 
GeneralRe: argh Pin
PIEBALDconsult17-May-12 11:15
mvePIEBALDconsult17-May-12 11:15 
AnswerRe: argh Pin
Luc Pattyn17-May-12 11:34
sitebuilderLuc Pattyn17-May-12 11:34 
GeneralRe: argh Pin
PIEBALDconsult17-May-12 11:36
mvePIEBALDconsult17-May-12 11:36 
QuestionAll(q=> q == q)? Pin
CodingBruce16-May-12 13:20
CodingBruce16-May-12 13:20 
GeneralRe: All(q=> q == q)? Pin
PIEBALDconsult16-May-12 13:57
mvePIEBALDconsult16-May-12 13:57 
AnswerRe: All(q=> q == q)? Pin
John Brett17-May-12 1:01
John Brett17-May-12 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.