2016-11-02 6 views
-1

次のコードをデバッグしようとしていますが、動作していません。私はこの写真 http://i68.tinypic.com/2rqoaqc.jpgこのエラーをデバッグする方法:「ラムダ式を文字列に変換できませんか?

をアップロード

は、これは私のコードです:

using System; 
using System.Text; 
using System.IO; 
using System.Linq; 
using System.Text.RegularExpressions; 

class Program 
{ 
    static void Main() 
    { 
     string input = "code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#gcoden"; 

     string output = Regex.Replace(
      input, 
      "#c(.*?)#g", 
      m => "#c" + m.Groups[1].Value.Split('+').Sum(int.Parse) + "#"); 

     Console.WriteLine(output);  
    } 
} 

そして、これらのエラー、私は取得しています、次のとおりです。

ERROR 1:

'int int.Parse(string)' has the wrong return type (CS0407) -

ERROR 2:

The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func)' and 'System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func)' (CS0121)

ERROR 3: - 文字列あなたがint.Parse()なく、明示的なラムダを使用する必要が

Cannot convert lambda expression to type 'string' because it is not a delegate type (CS1660)

+0

すべてのエラーメッセージを質問にコピーしてください。 – SLaks

+0

'** m **'はコード内で 'm'を強調表示しようとしていますか、それとも実際のコードですか? – spender

+0

はhightlightedです – jhonny625

答えて

3

にM "ラムダ" を変換できません:

string output = Regex.Replace(
     input, 
     "#c(.*?)#g", 
     m => "#c" + m.Groups[1].Value.Split('+').Sum(v => int.Parse(v)) + "#"); 

通知int.Parsev => int.Parse(v)に置き換えました。サンプルfiddle。興味深いことに

C#6.0で、必要に応じて、これはコンパイルと動作します:

string output = Regex.Replace(
     input, 
     "#c(.*?)#g", 
     m => "#c" + m.Groups[1].Value.Split('+').Sum(int.Parse) + "#"); 

サンプルRoslyn fiddleを。この変更がどこに記録されているのか分かりません。New Language Features in C# 6: Improved overload resolution

There are a number of small improvements to overload resolution, which will likely result in more things just working the way you’d expect them to. The improvements all relate to “betterness” – the way the compiler decides which of two overloads is better for a given argument.

One place where you might notice this (or rather stop noticing a problem!) is when choosing between overloads taking nullable value types. Another is when passing method groups (as opposed to lambdas) to overloads expecting delegates. The details aren’t worth expanding on here – just wanted to let you know!

+1

あなたは大変大歓迎です – jhonny625

関連する問題