Answer the question
In order to leave comments, you need to log in
Detach lambda method from event?
Hello everyone, I'm taking a course on c #
there is a line:
// Невозможно открепить ранее присоединенный анонимный метод.
instance.MyEvent -= delegate { Console.WriteLine("Анонимный метод 1."); };
Answer the question
In order to leave comments, you need to log in
stackoverflow.com/questions/183367/unsubscribe-ano...
Action myDelegate = delegate(){Console.WriteLine("I did it!");};
MyEvent += myDelegate;
// .... later
MyEvent -= myDelegate;
Для события только += или -=; Это для того и делается, чтобы защитить от шаловливых рук, чтобы не смогли извлечь из события и запускать вручную то, что привязано к событию.
И еще:
Action m1 = delegate { Console.WriteLine("Анонимный метод 1."); };
Action m2 = delegate { Console.WriteLine("Анонимный метод 1."); };
m1 == m2 //False
Я знаю два варианта, слабые ссылки или хранение и отписка.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question