是否有 C# 的 VB.NET 等效项??? 操作员?

发布于 2024-07-11 05:07:24 字数 47 浏览 4 评论 0原文

是否有与 C# 的 ?? 运算符等效的 VB.NET?

Is there a VB.NET equivalent for C#'s ?? operator?

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

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

发布评论

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

评论(6

我也只是我 2024-07-18 05:07:24

使用带有两个参数的 If() 运算符 (Microsoft 文档):

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again. 
Console.WriteLine(If(first, second))

first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

Use the If() operator with two arguments (Microsoft documentation):

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again. 
Console.WriteLine(If(first, second))

first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))
你的背包 2024-07-18 05:07:24

IF() 运算符应该可以为您解决问题:

value = If(nullable, defaultValueIfNull)

http: //visualstudiomagazine.com/listings/list.aspx?id=252

The IF() operator should do the trick for you:

value = If(nullable, defaultValueIfNull)

http://visualstudiomagazine.com/listings/list.aspx?id=252

信仰 2024-07-18 05:07:24

接受的答案没有任何解释,只是一个链接。
因此,我想我应该留下一个答案来解释 If 运算符如何工作,摘自 MSDN:


If 运算符 (Visual Basic)

使用短路评估有条件地返回两个之一
价值观。 If 运算符可以使用三个参数或两个参数来调用
论据。

If( [argument1,] argument2, argument3 ) 
  

如果使用两个参数调用运算符

If 的第一个参数可以省略。 这使得操作员
仅使用两个参数来调用。 以下列表适用
仅当使用两个参数调用 If 运算符时。

零件

Term         Definition
----         ----------

argument2    Required. Object. Must be a reference or nullable type. 
             Evaluated and returned when it evaluates to anything 
             other than Nothing.

argument3    Required. Object.
             Evaluated and returned if argument2 evaluates to Nothing.

当省略布尔参数时,第一个参数必须是
引用或可为 null 的类型。 如果第一个参数的计算结果为
,返回第二个参数的值。 在所有其他情况下,返回第一个参数的值。 这
以下示例说明了此评估的工作原理。

VB

' Variable first is a nullable type. 
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing 
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

如何处理两个以上值(嵌套 if)的示例:

Dim first? As Integer = Nothing
Dim second? As Integer = Nothing
Dim third? As Integer = 6
' The LAST parameter doesn't have to be nullable.
'Alternative: Dim third As Integer = 6

' Writes "6", because the first two values are "Nothing".
Console.WriteLine(If(first, If(second, third)))

The accepted answer doesn't have any explanation whatsoever and is simply just a link.
Therefore, I thought I'd leave an answer that explains how the If operator works taken from MSDN:


If Operator (Visual Basic)

Uses short-circuit evaluation to conditionally return one of two
values. The If operator can be called with three arguments or with two
arguments.

If( [argument1,] argument2, argument3 )

If Operator Called with Two Arguments

The first argument to If can be omitted. This enables the operator
to be called by using only two arguments. The following list applies
only when the If operator is called with two arguments.

Parts

Term         Definition
----         ----------

argument2    Required. Object. Must be a reference or nullable type. 
             Evaluated and returned when it evaluates to anything 
             other than Nothing.

argument3    Required. Object.
             Evaluated and returned if argument2 evaluates to Nothing.

When the Boolean argument is omitted, the first argument must be a
reference or nullable type. If the first argument evaluates to
Nothing, the value of the second argument is returned. In all other cases, the value of the first argument is returned. The
following example illustrates how this evaluation works.

VB

' Variable first is a nullable type. 
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing 
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

An example of how to handle more than two values (nested ifs):

Dim first? As Integer = Nothing
Dim second? As Integer = Nothing
Dim third? As Integer = 6
' The LAST parameter doesn't have to be nullable.
'Alternative: Dim third As Integer = 6

' Writes "6", because the first two values are "Nothing".
Console.WriteLine(If(first, If(second, third)))
我的黑色迷你裙 2024-07-18 05:07:24

您可以使用扩展方法。 这个方法的工作原理类似于 SQL COALESCE,对于您要测试的内容来说可能有些过大,但它确实有效。

    ''' <summary>
    ''' Returns the first non-null T based on a collection of the root object and the args.
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="args"></param>
    ''' <returns></returns>
    ''' <remarks>Usage
    ''' Dim val as String = "MyVal"
    ''' Dim result as String = val.Coalesce(String.Empty)
    ''' *** returns "MyVal"
    '''
    ''' val = Nothing
    ''' result = val.Coalesce(String.Empty, "MyVal", "YourVal")
    ''' *** returns String.Empty
    '''
    ''' </remarks>
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray args() As T) As T

        If obj IsNot Nothing Then
            Return obj
        End If

        Dim arg As T
        For Each arg In args
            If arg IsNot Nothing Then
                Return arg
            End If
        Next

        Return Nothing

    End Function

内置的 If(nullable, secondaryChoice) 只能处理两个可为空的选择。 在这里,我们可以根据需要合并任意数量的参数。 第一个非空参数将被返回,之后其余参数不再被求值(短路,如 AndAlso/&&或者Else/|| )

You can use an extension method. This one works like SQL COALESCE and is probably overkill for what you are trying to test, but it works.

    ''' <summary>
    ''' Returns the first non-null T based on a collection of the root object and the args.
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="args"></param>
    ''' <returns></returns>
    ''' <remarks>Usage
    ''' Dim val as String = "MyVal"
    ''' Dim result as String = val.Coalesce(String.Empty)
    ''' *** returns "MyVal"
    '''
    ''' val = Nothing
    ''' result = val.Coalesce(String.Empty, "MyVal", "YourVal")
    ''' *** returns String.Empty
    '''
    ''' </remarks>
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray args() As T) As T

        If obj IsNot Nothing Then
            Return obj
        End If

        Dim arg As T
        For Each arg In args
            If arg IsNot Nothing Then
                Return arg
            End If
        Next

        Return Nothing

    End Function

The built-in If(nullable, secondChoice) can only handle two nullable choices. Here, one can Coalesce as many parameters as desired. The first non-null one will be returned, and the rest of the parameters are not evaluated after that (short circuited, like AndAlso/&& and OrElse/|| )

孤星 2024-07-18 05:07:24

大多数这些解决方案的一个重要限制是它们不会短路。 因此,它们实际上并不等同于 ??

内置 If 运算符不会计算后续参数,除非前面的参数计算结果为空。

以下语句是等效的:

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;

VB

dim value = if(expression1,if(expression2,if(expression3,expression4)))

这将在 ?? 工作的所有情况下工作。 任何其他解决方案都必须极其谨慎地使用,因为它们很容易引入运行时错误。

The one significant limitation of most of these solutions is that they won't short-circuit. They are therefore not actually equivalent to ??.

The built-in If operator won't evaluate subsequent parameters unless the earlier parameter evaluates to nothing.

The following statements are equivalent:

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;

VB

dim value = if(expression1,if(expression2,if(expression3,expression4)))

This will work in all cases where ?? works. Any of the other solutions would have to be used with extreme caution, as they could easily introduce run-time bugs.

无妨# 2024-07-18 05:07:24

在此处查看有关 If 运算符 (Visual Basic) 的 Microsoft 文档:https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator

If( [argument1,] argument2, argument3 )

以下是一些示例 (VB.Net)

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

Check Microsoft documentation about If Operator (Visual Basic) here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator

If( [argument1,] argument2, argument3 )

Here are some examples (VB.Net)

' This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文