How do I convert the string to a Dictionary<string, uint>?
There are already answers to this & I tried this but it gives me error.
string abc = "key1=value1,key2=value2";
And below thing gives error:
var dict = text.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => split[1]);
I’m not sure how to store the value as uint in dictionary. Any suggestions?
Update: and at later point I want to convert this Dictionary<string, uint> back to a comma-separated string (key=value). How do I do that reverse conversion using linq?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You are very close – all you need is to parse uint for the value:
Dictionary<string,uint> dict = text.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => uint.Parse(split[1]));
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0