C# または VBNET で 2 つ (またはそれ以上) のリストを新しい一意のリストに結合するジェネリック関数を作成するにはどうすればよいですか?
リスト型の Join メソッドを見たことがありますが、その使用方法がわかりません。また、一度に複数のリストに参加できるかどうかもわかりません (そのため、Join の場合はジェネリック関数を要求しました)メソッドは仕事をすることができません)。
このようなもの:
private function JoinLists(of T)(byval Lists() as list(of T)) as list(of T)
dim newlist as new list(of T)
' some LINQ method or a For + list.Join()...
return joinedlists
end function
アップデート:
Anders Abel の提案を使用しようとしていますが、これは機能しません:
Private Function JoinLists(Of T)(ByVal Lists() As List(Of T)) As List(Of T)
Dim newlist As New list(Of T)
For Each l As List(Of T) In Lists
newlist.Concat(l)
Next
Return newlist
End Function
Dim a As New List(Of String) From {"a", "b"}
Dim b As New List(Of String) From {"f", "d"}
Dim newlist As List(Of String) = JoinLists(Of String)({a, b})
MsgBox(newlist.Count) ' Result: 0 Expected: 3
For Each item In newlist
MsgBox(item) ' Any item is shown
'expected:
'a
'b
'f
'd
Next