将 .GetValueOrDefault(0) 和 If(variable, 0) 与可空类型一起使用有什么区别吗?

发布于 2024-08-25 03:07:06 字数 235 浏览 8 评论 0原文

以下两种计算 c ...特别是装箱/拆箱问题的方法之间有什么区别吗?

Dim a As Integer? = 10
Dim b As Integer? = Nothing
Dim c As Integer

' Method 1
c = If(a, 0) + If(b, 0)

' Method 2
c = a.GetValueOrDefault(0) + b.GetValueOrDefault(0)

Is there any difference between the 2 methods below for calculating c ... specifically boxing/unboxing issues?

Dim a As Integer? = 10
Dim b As Integer? = Nothing
Dim c As Integer

' Method 1
c = If(a, 0) + If(b, 0)

' Method 2
c = a.GetValueOrDefault(0) + b.GetValueOrDefault(0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2024-09-01 03:07:06

根据 Reflector,代码片段中的 IL 反编译为:

Public Shared Sub Main()
    Dim a As Integer? = 10
    Dim b As Integer? = Nothing
    Dim c As Integer = (IIf(a.HasValue, a.GetValueOrDefault, 0) + IIf(b.HasValue, b.GetValueOrDefault, 0))
    c = (a.GetValueOrDefault(0) + b.GetValueOrDefault(0))
End Sub

[编辑] 然后查看反射函数 GetValueOrDefault()GetValueOrDefault(T defaultValue) 给出以下内容 (分别):

Public Function GetValueOrDefault() As T
    Return Me.value
End Function

并且

Public Function GetValueOrDefault(ByVal defaultValue As T) As T
    If Not Me.HasValue Then
        Return defaultValue
    End If
    Return Me.value
End Function

表明任何一种形式实际上都做同样的事情

According to Reflector, the IL from your code snippet decompiles into:

Public Shared Sub Main()
    Dim a As Integer? = 10
    Dim b As Integer? = Nothing
    Dim c As Integer = (IIf(a.HasValue, a.GetValueOrDefault, 0) + IIf(b.HasValue, b.GetValueOrDefault, 0))
    c = (a.GetValueOrDefault(0) + b.GetValueOrDefault(0))
End Sub

[EDIT] And then looking at the Reflected functions GetValueOrDefault() and GetValueOrDefault(T defaultValue) gives the following (respectively):

Public Function GetValueOrDefault() As T
    Return Me.value
End Function

and

Public Function GetValueOrDefault(ByVal defaultValue As T) As T
    If Not Me.HasValue Then
        Return defaultValue
    End If
    Return Me.value
End Function

Indicating either form does effectively exactly the same thing

梦屿孤独相伴 2024-09-01 03:07:06

c = If(a, 0) + If(b, 0) 语句被编译为:

  Dim tmpa As Integer
  If a.HasValue Then
    tmpa = a.GetValueOrDefault()
  Else
    tmpa = 0
  End If
  Dim tmpb As Integer
  If b.HasValue Then
    tmpb = b.GetValueOrDefault()
  Else
    tmpb = 0
  End If
  c = tmpa + tmpb

第二个片段完全按原样编译。它是这里明显的赢家。

The c = If(a, 0) + If(b, 0) statement gets compiled to this:

  Dim tmpa As Integer
  If a.HasValue Then
    tmpa = a.GetValueOrDefault()
  Else
    tmpa = 0
  End If
  Dim tmpb As Integer
  If b.HasValue Then
    tmpb = b.GetValueOrDefault()
  Else
    tmpb = 0
  End If
  c = tmpa + tmpb

The second snippet gets compiled exactly as-is. It is the clear winner here.

他不在意 2024-09-01 03:07:06

a.GetValueOrDefault(0)If(a, 0) 的稍微更高效的版本

a.GetValueOrDefault() 是稍微更高效的版本a.GetValueOrDefault(0) 的版本

当然,这只适用于数字类型。

a.GetValueOrDefault(0) is a slightly more efficient version of If(a, 0)

a.GetValueOrDefault() is a slightly more efficient version of a.GetValueOrDefault(0)

Of course, this is only true for numeric types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文