WinForms methods now throw ArgumentNullException

Some Windows Forms methods now throw an ArgumentNullException for null arguments, where previously they threw a NullReferenceException.

Change description

Previously, certain Windows Forms methods threw a NullReferenceException if passed an argument that was null. Starting in .NET 5, these methods now throw an ArgumentNullException for null arguments, instead.

Throwing an ArgumentNullException conforms to the behavior of the .NET runtime. It also improves the debugging experience by clearly communicating that an argument is null and which argument it is.

Version introduced

.NET 5.0

If you call any of these methods and your code currently catches a NullReferenceException for null arguments, catch an ArgumentNullException instead. In addition, consider updating the code to prevent passing null arguments to the listed methods.

Affected APIs

The following table lists the affected methods and parameters:

Method Parameter name Version added
Control.ControlCollection(Control) owner Preview 1
TabControl.GetToolTipText(Object) item Preview 1
TableLayoutControlCollection(TableLayoutPanel) container Preview 1
ToolStripRenderer.OnRenderArrow(ToolStripArrowRenderEventArgs) e Preview 1
ToolStripRenderer.OnRenderItemCheck(ToolStripItemImageRenderEventArgs) e Preview 1
ToolStripRenderer.OnRenderItemImage(ToolStripItemImageRenderEventArgs) e Preview 1
ToolStripRenderer.OnRenderItemText(ToolStripItemTextRenderEventArgs) e Preview 1
ToolStripRenderer.OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs) > e Preview 1
DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyle) dataGridViewCellStyle Preview 2
RichTextBox.LoadFile(Stream, RichTextBoxStreamType) data Preview 2
ListBox.IntegerCollection(ListBox) owner Preview 5
ListBox.IntegerCollection.CopyTo(Array, Int32) destination Preview 5
ListViewGroup.ISerializable.GetObjectData(SerializationInfo, StreamingContext) info Preview 5
VisualStyleRenderer(String, Int32, Int32) className Preview 5
ListBox.ObjectCollection(ListBox) owner Preview 6
ListBox.ObjectCollection(ListBox, Object[]) owner, value Preview 6
ListBox.ObjectCollection(ListBox, ListBox+ObjectCollection) owner, value Preview 6
ListBox.ObjectCollection.AddRange(Object[]) items Preview 6
ListBox.ObjectCollection.AddRange(ListBox+ObjectCollection) value Preview 6
ListBox.ObjectCollection.CopyTo(Object[], Int32) destination Preview 6
ListBox.ObjectCollection.ICollection.CopyTo(Array, Int32) destination Preview 6
ListView.SelectedIndexCollection(ListView) owner Preview 7
TreeNodeCollection.Find(String, Boolean) key is null or empty Preview 8
ListView.ListViewItemCollection.Find(String, Boolean) key is null or empty RC1
ScrollableControl.OnPaintBackground(PaintEventArgs) e RC1