添加运算符时出现c#console.writeline错误+"新"

时间:2016-03-06 10:33:26

标签: c# string string-matching console.writeline method-group

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

  

运营商' +'不能应用于类型'方法组的操作数'和   '串'

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

1 个答案:

答案 0 :(得分:2)

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

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

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