gpt4 book ai didi

vb.net - 属性 setter 中 ArgumentException 使用什么参数名称

转载 作者:行者123 更新时间:2023-12-02 04:10:36 25 4
gpt4 key购买 nike

来自 MSDN:

Every ArgumentException should carry the name of the parameter that causes this exception.

我的问题:如果属性 setter 应该抛出 ArgumentException,我应该给它 setter 的参数名称(默认值:value)还是属性名称?

示例:

    Private _myProperty As String
Public Property MyProperty As String
Get
Return _myProperty
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then
' what I've been doing for the last 2 years
Throw New ArgumentNullException("value", "value cannot be empty")

' what I think I should be doing instead
Throw New ArgumentNullException("MyProperty", "value cannot be empty")
End If
_myProperty = value
End Set
End Property

我希望这是有道理的。你觉得怎么样?

编辑

我猜另一个解决方案是将 value 重命名为更有意义的名称,然后将其用作 paramName 的值。但不知何故,这似乎不是正确的做法。

最佳答案

根据 Catching and Throwing Standard Exception Types 中的示例在 MSDN 上,您应该继续将“value”设置为参数名称:

Do use value for the name of the implicit value parameter of property setters.

所以没关系:

Throw New ArgumentNullException("value", "value cannot be empty")

关于vb.net - 属性 setter 中 ArgumentException 使用什么参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15123083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com