简体   繁体   中英

C# Lambda expression -Help

I am learning lambda expression and delegates.While i try to execute the following ,I am getting error at the line which is marked bold line. ( Error : Operator '+=' cannot be applied to operands of type 'Test.MessageDelegate' and 'lambda expression' ).Help me to handle lambda expression.

namespace Test
{
    public delegate void MessageDelegate(string title,object sender,EventArgs e);
    class Program
    {
        static event MessageDelegate logEvent;

        static void Main(string[] args)
        {
            logEvent = new MessageDelegate(OnLog);
            logEvent("title",Program.logEvent,EventArgs.Empty);
  Console.ReadKey(true); } static void OnLog(string title, object sender, EventArgs e) { if (logEvent != null) { Console.WriteLine("title={0}", title); Console.WriteLine("sender={0}", sender); Console.WriteLine("arguments={0}",e.GetType()); } } } } 

Since logEvent has MessageDelegate as its event handler, you'd need the left hand of the lambda expression (src, e) to match the signature of MessageDelegate

Change to (str, src, e) => OnLog(str, src, e)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM