You are here
Home > Software Development > DotNet > C# > C# CheatSheet

C# CheatSheet

Creating a comma separated string from IList<string> or IEnumerable<string>

IList<string> strings = new List<string>{“1″,”2″,”testing”};
string joined = string.Join(“,”, strings);

 

Creating a comma separated string from IList<int> or IEnumerable<int>

List<int> list =List<int>{1,2,3};
string joined = string.Join(“,”, list.Select(n => n.ToString()).ToArray())

 

Converting a comma separated string  to IList<int> or IEnumerable<int>

string commaSeparatedString = “1,2,3”;
List<int> list = Array.ConvertAll(commaSeparatedString .Split(‘,’), int.Parse).ToList();
or
string commaSeparatedString = “1,2,3”;
List<int> list = s.Split(‘,’).Select(int.Parse).ToArray().ToList();

Leave a Reply

Top