How to create a List<T> from a comma separated string?

Posted by Nick Allen - Tungle139 on Stack Overflow See other posts from Stack Overflow or by Nick Allen - Tungle139
Published on 2009-05-26T10:59:28Z Indexed on 2010/05/08 16:58 UTC
Read the original article Hit count: 160

Filed under:
|

Given the variable

string ids = Request.QueryString["ids"]; // "1,2,3,4,5";

Is there any way to convert it into a List without doing something like

List<int> myList = new List<int>();

foreach (string id in ids.Split(','))
{
    if (int.TryParse(id))
    {
        myList.Add(Convert.ToInt32(id));
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics