Вопрос

I want to conditionally unhook an event handler. Is this the right way to do it:

tb.TextChanged -= textBoxIntName_TextChanged;

?

This seems to be sensible, as hooking it up required:

tb.TextChanged += textBoxIntName_TextChanged;

...but it also seems that what corresponds to the "Delphi way" makes as much or more sense (but alas, it does not compile):

tb.TextChanged = nil;
Это было полезно?

Решение

You cannot assign events - only attach (+=) and remove (-=) operations are available for clients.

Read more about events here.

Also C# specification says:

Since += and -= are the only operations that are permitted on an event outside the type that declares the event, external code can add and remove handlers for an event, but cannot in any other way obtain or modify the underlying list of event handlers.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top