[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Determine whether a year is a leap year in C#

[Determine whether a year is a leap year in C#]

Detecting leap years is practically trivial because the DateTime data type provides an IsLeapYear method to do just this. The example uses the following code to display the leap years between the two entered dates.

// List leap years between the two entered years. private void btnList_Click(object sender, EventArgs e) { lstYears.Items.Clear(); int from_year = int.Parse(txtFromYear.Text); int to_year = int.Parse(txtToYear.Text); for (int year = from_year; year <= to_year; year++) { if (DateTime.IsLeapYear(year)) lstYears.Items.Add(year); } }

The code uses int.Parse to convert the entered year numbers from strings into integers. It then loops over the years within the indicated range and adds a year to the ListBox if DateTime.IsLeapYear returns true.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.