2016-11-02 2 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) -

오류 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)

오류 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.

는 흥미롭게도,이 컴파일 # 6.0 C로 원하는대로 작품 :

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

관련 문제