Reputation: 2593
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);
logEvent += (src, e) => { OnLog("Some",src, e); };
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());
}
}
}
}
Upvotes: 1
Views: 460
Reputation: 129832
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)
Upvotes: 5