我刚刚学习如何在 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" }
现在,我想在条件中使用文字数组(参见伪代码)
If 1 exists in {1,2,3,4} Then
MsgBox "Exists!"
End If
但我不知道怎么做,似乎你必须先将它分配给一个变量,然后才能在条件中使用它。
Dim MyArray() As Integer = {3, 2, 3}
If (MyArray.Contains(1)) Then
MsgBox("exists!")
Else
MsgBox("does not exist!")
End If
上面的代码有效,但我只是想知道有没有办法在不先将数组文字分配给变量的情况下执行此操作?
提前致谢!
最佳答案
为此使用 {1,2,3,4}.Contains(1)
。
关于VB.NET - 如何在条件中使用数组文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907002/