Convert Dictionary into structured format string
I would have written the whole method block with Linq like this (sorry for the C#-vb.net soup...)
c-sharp
return String.Join(",",Me._set.Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray());
Also, I don't really know what _set is. Maybe you'll have to cast :
c-sharp:
return String.Join(",", Me._set.Cast<KeyValuePair<String,String>>().Select(kvp=>String.Format("{0}={1}",kvp.Key, kvp.Value).ToArray());
vb.net:
return String.Join(", ", Me.Select(Function(kvp) String.Format("{0}={1}", kvp.Key, kvp.Value)).ToArray())
Hope this will help,
VB.net syntax:
Dim dic As New Dictionary(Of String, String)() From {
{"a", "1"},
{"b", "2"},
{"c", "3"},
{"d", "4"}
}
Dim s As String = String.Join(",", dic.Select(Function(pair) String.Format("{0}={1}", pair.Key, pair.Value)).ToArray())
As far as your non-LINQ loop goes, I would recommend doing it like this:
Public Overrides Function ToString() As String
Dim items As New List(Of String)(_set.Count)
For Each pair As KeyValuePair(Of String, String) In _set
items.Add($"{pair.Key}={pair.Value}"))
Next
Return String.Join(", ", items)
End Function
With LINQ, you could do it like this:
Public Overrides Function ToString() As String
Return String.Join(", ", _set.Select(Function(pair) $"{pair.Key}={pair.Value}"))
End Function