Change items in a list using ForEach lambda

I want to filter an array of strings:

string[] args 

from the command line, for example: "-command1 xy -command2 ab -command3 cd"

First, all words with the symbol '-', and then convert them to uppercase.

 var commands = args.Where(x => x.StartsWith("-")).ToList<String>(); commands.ForEach(x => { x.ToUpper() }); commands.ToString(); 

This will return a list of args with words starting with a lowercase "-" - that is, lambda is not applied. Why is this? Is a copy of the list for lambda capture, and which is modified, not the Origina list itself?

+5
source share
1 answer
 var commands = args.Where(x => x.StartsWith("-")).Select(y => y.ToUpper()).ToList(); 

or

 var upperCommands = new List<String>(); var commands = args.Where(x => x.StartsWith("-")).ToList<String>(); commands.ForEach(x => upperCommands.Add( x.ToUpper()); 
+5
source

All Articles