I have to bind a drop down box with years starting from 2008 to current year in C#. How can I achieve it.
You can build a sequence of integers with System.Linq.Enumerable.Range:
var startYear = 2008;
myDropDownList.DataSource = Enumerable.Range(startYear, DateTime.Now.Year - startYear + 1);
myDropDownList.DataBind();
Enumerable.Range on MSDN
Update: In .NET 2.0 you can implement your own Range operator with an iterator:
public static IEnumerable<int> Range (int start, int count)
{
int end = start + count;
for (int i = start; i < end; i++)
yield return i;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With