2016-03-06 186 views
1

我使用下面的代碼使用,我想一個字符串添加到輸出,但我得到這個錯誤C#console.writeline錯誤操作+「新」

運營商「+」不能應用於「方法組」類型的操作數和 「字符串」。

 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 


     } 


} 

這裏是類

回答

2

如果你想使用WriteLine方法之前修改字符串,你有兩種可能性:

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

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

它的工作大。 但在我的大代碼,它只有當我這樣做的時候: (「」+ s)。 (s +「」)其他人正在顯示「」裏面的內容,只有 –

+0

,原因可能是什麼。 –

+0

我也不明白什麼。選擇做。 非常感謝 –