VB.NET - 如何在条件中使用数组文字?

dpp*_*dpp 4 vb.net arrays literals conditional-statements

我只是学习如何在VB.NET中创建数组文字.

Dim MyArray = New Integer() { 1, 2, 3 }
' Or
Dim MyArray() As Integer = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, 3 }
' Or
Dim MyArray() = { 1, 2, "A", "B" }
Run Code Online (Sandbox Code Playgroud)

现在,我想在条件中使用A LITERAL ARRAY(参见伪代码)

If 1 exists in {1,2,3,4} Then
    MsgBox "Exists!"
End If
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么样,似乎你必须先将它分配给变量才能在条件中使用它.

    Dim MyArray() As Integer = {3, 2, 3}
    If (MyArray.Contains(1)) Then
        MsgBox("exists!")
    Else
        MsgBox("does not exist!")
    End If
Run Code Online (Sandbox Code Playgroud)

上面的代码工作,但我只是想知道有没有办法做到这一点,而不首先将数组文字赋值给变量?

提前致谢!

jon*_*sca 5

使用{1,2,3,4}.Contains(1)此.

  • 如果答案是蛇,我本可以被它杀死,它已经在我面前了! (2认同)