c# console.writeline 添加运算符+“new”时出错

c# console.writeline error when adding the operator + " new"

我正在使用下面的代码,我想在输出中添加一个字符串,但是我得到了这个错误

operator '+' cannot be applied to operands of type 'method group' and 'string'.

     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


        }


}

这里是 class

public class myclass
    {
         public List<string> variableX = new List<string>();
    }

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

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

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