我正在使用以下代码,我想在输出中添加一个字符串,但是我收到此错误
运营商' +'不能应用于类型'方法组的操作数'和 '串'
class Program
{
static void Main(string[] args)
{
string sample = "1a2b3c4d";
MatchCollection matches = Regex.Matches(sample, @"\d");
List<myclass> matchesList = new List<myclass>();
foreach (Match match in matches)
{
myclass class1 = new myclass();
class1.variableX.Add(match.Value);
matchesList.Add(class1);
}
foreach (myclass class2 in matchesList)
class2.variableX.ForEach(Console.WriteLine + " "); // <---- ERROR
}
}
这是班级
public class myclass
{
public List<string> variableX = new List<string>();
}
答案 0 :(得分:2)
如果要在使用WriteLine
方法之前修改字符串,则有两种可能性:
class2.variableX.ForEach(s => Console.WriteLine(s + " "));
或
class2.variableX.Select(s => s + " ").ForEach(Console.WriteLine);