我有以下代碼(對不起,VB!),我正在尋找避免必須做一個For Each
循環來初始化String
(即「10,25,50」)收集使用Int32.TryParse
。任何方式來初始化使用Int32.TryParse列表(整數)
有沒有辦法做到這一點?像Function(x)
lambda那種只在String
集合中通過Int32.TryParse
時才添加物品的東西?
Dim options = ConfigHelper.GetContentValue("NavigationPaging", "PageSizeOptions").Split(CChar(","))
Dim optionsList As List(Of Integer) = New List(Of Integer)() 'Initialise the collection here to avoid the For Each
For Each item As String In options
Dim i As Integer
If Int32.TryParse(item, i) Then
optionsList.Add(i)
End If
Next
謝謝。
什麼選擇什麼樣子的? 「 – Plutonix
」10,25,50「我會在這個問題上加上。 –
TryParse是爲了確保沒有duff值拋出異常。有很多人可以修改原始字符串。 –