I had a need for a method that could take a collection of strings, and replace all occurrences of a specific string with another.
For example, if I have a List<string> that looks like this:
List<string> strings = new List<string> { "a", "b", "delete", "c", "d", "delete" };
and I want to replace “delete” with “”, I would use this LINQ statement:
strings = (from s in strings select (s=="delete" ? s=String.Empty : s)).ToList();
and it works great. But then I figured I should make it an extension method, since I’d likely use it again later. In this case, I just want to write the following:
strings.ReplaceStringInListWithAnother( "delete", String.Empty);
While my code compiles, and the LINQ statement works inside of the extension method, when I return the collection reverts back to its original contents:
public static void ReplaceStringInListWithAnother( this List<string> my_list, string to_replace, string replace_with)
{
my_list = (from s in my_list select (s==to_replace ? s=replace_with : s)).ToList();
}
So it would seem that I just modified a copy of the List… but when I looked at the code for Pop, it modifies the collection similarly, yet the changes stick, so my assumption was that my method’s parameter declarations are correct.
Can anyone explain what I am doing wrong here?
The LINQ statement you wrote does not modify the collection, it actually creates a new one.
The extension method you wrote creates this new collection and then discards it. The assignment is redundant: you’re assigning to a local parameter, which goes out of scope immediately after.
When you’re calling the method, you’re also discarding its result instead of assigning it back.
Therefore, you should write the method like this:
and the call like this:
By the way, you can make the function more useful by making it generic:
This way you can use it for other things, not just
strings. Furthermore, you can also declare it to useIEnumerable<T>instead ofList<T>:This way you can use it for any collection of equatable items, not just
List<T>. Notice thatList<T>implementsIEnumerable<T>, so you can still pass a List into this function. If you want a list out, simply call.ToList()after the call to this one.Update: If you actually want to replace elements in a list instead of creating a new one, you can still do that with an extension method, and it can still be generic, but you can’t use Linq and you can’t use
IEnumerable<T>:This will not return the new list, but instead modify the old one, so it has a void return type like your original.