WinForms properties now throw ArgumentOutOfRangeException

Some Windows Forms properties now throw an ArgumentOutOfRangeException for invalid arguments, where previously they did not.

Change description

Previously, these properties threw various exceptions, such as NullReferenceException, IndexOutOfRangeException, or ArgumentException, when passed out-of-range arguments. Starting in .NET 5, these properties now throw an ArgumentOutOfRangeException when passed arguments that are out of range.

Throwing an ArgumentOutOfRangeException conforms to the behavior of the .NET runtime. It also improves the debugging experience by clearly communicating which argument is invalid.

Version introduced

.NET 5.0

Affected APIs

The following table lists the affected properties and parameters:

Property Parameter name Version added
ListBox.IntegerCollection.Item[Int32] index 5.0 Preview 5
TreeNode.ImageIndex value 5.0 Preview 6
TreeNode.SelectedImageIndex value 5.0 Preview 6