0

I want use a Lambda Expression but get an error which occurs on the line commented below, when I try to call it.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace ConsoleAppTestDelegate2
 {
 public delegate string MyDelegate (int a);
 public class ClassRunDelegate
 {
 public void RunDelegate(MyDelegate a, int b)
 {
 Console.WriteLine(a(b));
 }
 }
 public class MyHelp
 {
 public string test(int a)
 {
 a++;
 return a.ToString();
 }
 }
 class Program
 {
 static void Main(string[] args)
 {
 MyHelp fhelp = new MyHelp();
 //
 MyDelegate fdelegate = new MyDelegate(fhelp.test);
 ClassRunDelegate cc = new ClassRunDelegate();
 cc.RunDelegate(fdelegate, 10); 
 ///
 cc.RunDelegate((a, b) => { Console.WriteLine("test"); });// get error this line
 Console.ReadLine();
 }
 }
 }
George Duckett
32.6k12 gold badges101 silver badges167 bronze badges
asked May 1, 2013 at 11:14
2
  • What does the error say? Commented May 1, 2013 at 11:16
  • That line isn't even valid. I don't know what you are trying to do. Commented May 1, 2013 at 11:17

1 Answer 1

1

From your code, MyDelegate should return string, but Console.WriteLine("test") does not return anything, so that does not compile:

 cc.RunDelegate((a) => { Console.WriteLine("test"); }, b);

You should either return something after Console.WriteLine or use another type of delegate, with no return value.

answered May 1, 2013 at 11:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.