c# - Raising events by calling delegates - why not by event name? -
i have looking @ old piece of code , cannot somehow understand following:
public event eventhandler namechanged; #endregion #region protected void onnamechanged(eventargs args) /// <summary> /// raises namechanged event. /// </summary> /// <param name="args">event arguments.</param> protected void onnamechanged(eventargs args) { eventhandler eh = this.namechanged; if (eh != null) { eh(this, args); } }
why event raised invocation of delegate? not call event (namechanged) usual?
edit: can see suggested on msdn: https://docs.microsoft.com/en-us/dotnet/standard/events/
whenever reference event, in fact copying invocation list local reference. doing that, make sure between checking validity of event eh != null
, invoking event eh(this, args)
eh's value wouldn't change (maybe different thread).
in c# 6, there new operator https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators
you instead:
namechanged?.invoke(this, args);
Comments
Post a Comment