某些 API 會擲回 ArgumentNullException (.NET 7)

有一些 API 現在會驗證輸入參數,並擲回 ArgumentNullException。之前是在使用 null 輸入引數叫用這些 API 時,才會擲回 NullReferenceException

先前的行為

在舊版 .NET 中,如果使用 null 引數叫用受影響的 API,其會擲回 NullReferenceException

新的行為

從 .NET 7 開始,如果使用 null 引數叫用受影響的 API,其會擲回 ArgumentNullException

變更類別

此變更會影響二進位相容性

變更原因

擲回 ArgumentNullException 符合 .NET 執行階段的行為。 因為會明確傳達導致例外狀況的引數,所以也提升了偵錯的體驗。

導入的版本

.NET 7

  • 如有必要,請檢查及更新您的程式碼,避免傳遞 null 輸入引數給受影響的 API。
  • 若您的程式碼會處理 NullReferenceException,請置換 ArgumentNullException,或為其新增其他處理常式。

受影響的 API

下表列出受影響的 API 和其專用參數。

方法/屬性 參數名稱 變更版本
ComboBox.ChildAccessibleObject(ComboBox, IntPtr) owner Preview 1
ControlPaint.CreateHBitmap16Bit(Bitmap, Color) bitmap Preview 1
ControlPaint.CreateHBitmapColorMask(Bitmap, IntPtr) bitmap Preview 1
DataGridViewEditingControlShowingEventArgs(Control, DataGridViewCellStyle) controlcellStyle Preview 1
ToolStripArrowRenderEventArgs(Graphics, ToolStripItem, Rectangle, Color, ArrowDirection) g Preview 1
ToolStripContentPanelRenderEventArgs(Graphics, ToolStripContentPanel) gcontentPanel Preview 1
ToolStripItemRenderEventArgs(Graphics, ToolStripItem) gitem Preview 1
ToolStripPanelRenderEventArgs(Graphics, ToolStripPanel) gtoolStripPanel Preview 1
ListView.CheckedIndexCollection(ListView) owner Preview 5