Friday, March 23, 2007

Entirely redundant C# 2.0 code

private static IEnumerable< int> Range ( int start, int end )
{
for ( int i = start; i != end; i = (start > end ) ? i - 1 : i + 1 )
{
yield return i;
}
yield return end;
}

private void PopulateDaysDropDown ( )
{
foreach ( int day in Range ( 1 , 31 ) )
{
DateOfBirthDay.Items.Add ( day.ToString ( ) );
}
}
The only benefit of this is Range accepts start > end, so I could equally well do
foreach ( int day in Range ( 31, 1 ) ) {  //... }
I'm determined to find a valid use for yield return somewhere!

No comments: