这个ForEach循环出了什么问题?

uno*_*nom 1 c# foreach

是的......那是其中的一天.

public string TagsInput { get; set; }

//further down
var tagList = TagsInput.Split(Resources.GlobalResources.TagSeparator.ToCharArray()).ToList();
tagList.ForEach(tag => tag.Trim()); //trim each list item for spaces
tagList.ForEach(tag => tag.Replace(" ", "_")); //replace remaining inner word spacings with _
Run Code Online (Sandbox Code Playgroud)

两个ForEach循环都不起作用.tagList只是一个List.

谢谢!

Ano*_*on. 5

Trim()并且Replace()不要修改它们被调用的字符串.他们创建了一个新的字符串,该字符串已应用了该操作.

你想要使用Select,而不是ForEach.

tagList = tagList.Select(t => t.Trim()).Select(t => t.Replace(" ", "_")).ToList();
Run Code Online (Sandbox Code Playgroud)