我刚学会了如何在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
上面的代码可以正常运行,但是我想知道有没有不用先把数组字面量赋值给变量的方法呢?
提前谢谢!
(New Integer() {3, 1, 3}).Contains(1)
,它更加优美。谢谢! - kazinix