c# string.replace in foreach loop

c#, foreach, string

Solution

You say you're after a LINQ solution... that's easy:

var replacedNames = names.Select(x => x.Replace("pdf", "txt"));

We don't know the type of `names`, but if you want to assign back to it you could potentially use `ToArray` or `ToList`:

// If names is a List<T>
names = names.Select(x => x.Replace("pdf", "txt")).ToList();
// If names is an array
names = names.Select(x => x.Replace("pdf", "txt")).ToArray();

You should be aware that the code that you've posted isn't using LINQ at all at the moment though...

Problem

Somehow I can't seem to get string replacement within a foreach loop in C# to work. My code is as follows : ``` foreach (string s in names) { s.Replace("pdf", "txt"); } ``` Am still quite new to LINQ so pardon me if this sounds amateurish ;)

Original source