Consider this:
for( int i = list.Count – 1 ; i >= list.Count – 50 ; –i , )
. array [ i ] = list [ i ];
Intention was the get the last 50 terms from list. Ignoring the facts that it will throw a null reference exception if the list is null and an index-out-of-range exception if it contains no elements it also fails to copy all 50 elements. The condition is to be blamed.
Correct way is not to invent your own logic and use what the framework offers.
if( list != null && list.Count > 50 )
list.RemoveRange( 50 , list.Count – 50 );
For added bonus, consider this paging code.
for( int i = 0 , index = 0 ; i < some_huge_number ; ++i )
{
page.Results.Add( results [ i ] );
if( index == 10 )
{
pages.Add( page );
page = new Page( );
index = 0;
}
++index;
}
This code strives to divide results into pages each with 10 results. Can you imagine what this will do if there were only 9 results?
It will proceed to add those results to page, then, do nothing. Because of the incorrect if condition. Again, the correct way is divide-and-remainder approach as demonstrated here.