score:9

Accepted answer

Just use a regular for loop - that's the simplest way of modifying the collection:

for (int i = 0; i < list.Count; i++)
{
    string url = list[i];
    if (!url.StartsWith("http://"))
    {
        list[i] = "http://" + url;
    }
}

If you're happy to create a new collection, it's simple:

var modifiedList = list.Select(url => url.StartsWith("http://") ? url : "http://" + url)
                       .ToList();

score:3

yourlist.Where(_ => !_.StartsWith("http://")).ToList().ForEach(_ => _.Insert(0, "http://"));

score:1

Another way to do this by using Union is:

var result =
    list.Where(url => url.StartsWith("http://"))
        .Union(list.Where(url => !url.StartsWith("http://")).Select(url => $"http://{url}"))
        .ToList();

Related Articles