1

我正在使用以下代码,我想在输出中添加一个字符串,但我收到此错误

运算符“+”不能应用于“方法组”和“字符串”类型的操作数。

     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>();
    }
4

1 回答 1

2

如果您想在使用WriteLine方法之前修改字符串,您有两种可能性:

  • class2.variableX.ForEach(s => Console.WriteLine(s + " "));

或者

  • class2.variableX.Select(s => s + " ").ForEach(Console.WriteLine);
于 2016-03-06T10:37:40.930 回答