{"id":269793,"date":"2015-12-07T17:23:02","date_gmt":"2015-12-07T14:23:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=269793"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=269793","title":{"rendered":"\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0431\u0435\u0437 \u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f \u0432 C# 6"},"content":{"rendered":"<br \/>\n<h2>\u041e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430<\/h2>\n<p>  \u0427\u0430\u0441\u0442\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e\u0442, \u0437\u0430\u0447\u0435\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0430 \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u0434 \u0440\u0435\u0432\u044c\u044e, \u0434\u0430\u0436\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u043e\u0431 \u044d\u0442\u043e\u043c. \u0412 C# 6 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u044f\u0437\u044b\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 null-conditional operator (null-\u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0438\u043b\u0438 \u042d\u043b\u0432\u0438\u0441-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u2014 <b>?.<\/b>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0433\u043e (\u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434) \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043e\u0442 \u0414\u0436\u043e\u043d\u0430 \u0421\u043a\u0438\u0442\u0430 \u2014 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0434\u043e\u0442 \u043d\u0435\u0442 \u0433\u0443\u0440\u0443.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/h2>\n<p>  \u0412\u044b\u0437\u043e\u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0432 \u044f\u0437\u044b\u043a\u0435 C# \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043e\u043f\u0440\u043e\u0432\u043e\u0436\u0434\u0430\u043b\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u044b\u043c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0435, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u0432 \u0432\u0438\u0434\u0435 null \u0441\u0441\u044b\u043b\u043a\u0438. \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0438\u0441\u0430\u043b\u0438 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cs\">public event EventHandler Foo;   public void OnFoo() {     EventHandler handler = Foo;     if (handler != null)     {         handler(this, EventArgs.Empty);     }    } <\/code><\/pre>\n<p>  \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e handler \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0431\u0435\u0437 \u043d\u0435\u0435 \u043a \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u044f Foo \u0434\u043e\u0441\u0442\u0443\u043f \u0438\u0434\u0435\u0442 2 \u0440\u0430\u0437\u0430 (\u043f\u0440\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043d\u0430 null \u0438 \u043f\u0440\u0438 \u0441\u0430\u043c\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435). \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u0442\u044c \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a \u0443\u0434\u0430\u043b\u0438\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430\u043c\u0438 \u043a Foo.  <\/p>\n<pre><code class=\"cs\">\/\/ \u041f\u043b\u043e\u0445\u043e\u0439 \u043a\u043e\u0434, \u043d\u0435 \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0442\u0430\u043a! if (Foo != null) {     \/\/ Foo \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c null, \u0435\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f     \/\/ \u043a \u043a\u043b\u0430\u0441\u0441\u0443 \u0438\u0434\u0435\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432.     Foo(this, EventArgs.Empty); } <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c, \u0441\u043e\u0437\u0434\u0430\u0432 \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"cs\">public static void Raise(this EventHandler handler, object sender, EventArgs args) {     if (handler != null)     {         handler(sender, args);     }    } <\/code><\/pre>\n<p>  \u0422\u043e\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u0442\u0441\u044f:  <\/p>\n<pre><code class=\"cs\">public void OnFoo() {     Foo.Raise(this, EventArgs.Empty); } <\/code><\/pre>\n<p>  \u041c\u0438\u043d\u0443\u0441 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.<\/p>\n<h2>C# 6 \u043d\u0430\u0441 \u0441\u043f\u0430\u0441\u0435\u0442!<\/h2>\n<p>  Null-\u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 (<b>?.<\/b>), \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u0432 C# 6, \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c, \u043d\u043e \u0438 \u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"cs\">public void OnFoo() {     Foo?.Invoke(this, EventArgs.Empty); } <\/code><\/pre>\n<p>  \u0423\u0440\u0430! \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442 NullReferenceException, \u0438 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u044b \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b.<\/p>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0431\u044b\u043b\u043e \u0431\u044b \u043b\u0443\u0447\u0448\u0435, \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c Foo?(this, EventArgs.Empty), \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0431\u044b\u043b \u0431\u044b \u0443\u0436\u0435 \u043d\u0435 <b>?.<\/b> \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u0447\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043b\u043e \u0431\u044b \u044f\u0437\u044b\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 Invoke \u043c\u0435\u043d\u044f \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442.<\/p>\n<h2>\u0427\u0442\u043e \u044d\u0442\u043e \u0437\u0430 \u0448\u0442\u0443\u043a\u0430 \u2014 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c?<\/h2>\n<p>  \u041d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u043a\u043e\u0434 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u00ab\u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u043c\u00bb \u0432 \u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u0447\u0442\u043e \u0435\u043c\u0443 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u044e\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u2014 \u043c\u044b \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c NullReferenceException. \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u043b\u0438 \u043e\u0442\u043c\u0435\u043d\u044f\u044e\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443 \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u0430\u043c\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u042d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437-\u0437\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>  \u0412 C# 4 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 Interlocked.CompareExchange, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 Interlocked.CompareExchange, \u0447\u0442\u043e\u0431\u044b \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0430\u043c\u043e\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u044d\u0442\u0438 2 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c:  <\/p>\n<pre><code class=\"cs\">public void OnFoo() {     Interlocked.CompareExchange(ref Foo, null, null)?.Invoke(this, EventArgs.Empty); } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0431\u0435\u0437 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f <i>\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e<\/i> \u043a\u043e\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0432\u0435\u0434\u043e\u043c\u0438\u0442\u044c \u0441\u0430\u043c\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432, \u0431\u0435\u0437 \u0440\u0438\u0441\u043a\u0430 \u0441\u0432\u0430\u043b\u0438\u0442\u044c\u0441\u044f \u0441 NullReferenceException. \u0421\u043f\u0430\u0441\u0438\u0431\u043e <a href=\"https:\/\/twitter.com\/davidfowl\">David Fowler<\/a> \u0437\u0430 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u044b\u0437\u043e\u0432 CompareExchange \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u043a\u0440\u0430\u0441\u0438\u0432\u043e. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 .NET 4.5 \u0438 \u0432\u044b\u0448\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/gg712828.aspx\">Volatile.Read<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 <i>\u043c\u043e\u0436\u0435\u0442<\/i> \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043d\u043e \u043c\u043d\u0435 \u043d\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u044f\u0441\u043d\u043e (\u0435\u0441\u043b\u0438 \u0447\u0438\u0442\u0430\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e), \u0434\u0435\u043b\u0430\u0435\u0442 \u043b\u0438 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e. (\u0412 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u043e\u043d \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0447\u0442\u0435\u043d\u0438\u044f\/\u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u0432 \u043d\u0430\u0448\u0435\u043c \u0436\u0435 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0441\u0442\u0430\u0432\u0438\u0442\u044c <i>\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435<\/i> \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 <i>\u043f\u043e\u0441\u043b\u0435<\/i> \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f).  <\/p>\n<pre><code class=\"cs\">public void OnFoo() {     \/\/ .NET 4.5+, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u043d\u0435 \u0431\u044b\u0442\u044c...     Volatile.Read(ref Foo)?.Invoke(this, EventArgs.Empty); } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044f \u043d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043b. \u041f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0441\u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u0432\u0435\u0440\u0435\u043d \u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u043b \u0432 BCL.<\/p>\n<h2>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434<\/h2>\n<p>  \u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c: \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0443\u0441\u0442\u043e\u0439 \u0444\u0438\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043e\u0434\u043d\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0443 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043b\u044f\u043c\u0431\u0434\u0430-\u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432:  <\/p>\n<pre><code class=\"cs\">public event EventHandler Foo = delegate {}   public void OnFoo() {     \/\/ Foo will never be null     Volatile.Read(ref Foo).Invoke(this, EventArgs.Empty);    } <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0435 \u0441\u0430\u043c\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432, \u043d\u043e \u0437\u0430\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u043d\u0430\u0434\u043e \u0432\u043e\u043b\u043d\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043d\u0430 null \u0438 NullReferenceException.<\/p>\n<h2>\u0418\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c MSIL<\/h2>\n<p>  <i>\u041e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430:<\/i> \u044d\u0442\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043d\u0435\u0442 \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \u0414\u0436\u043e\u043d\u0430, \u044d\u0442\u043e \u043c\u043e\u0438 \u043b\u0438\u0447\u043d\u044b\u0439 \u0438\u0437\u044b\u0441\u043a\u0430\u043d\u0438\u044f \u0432 ildasm&#8217;\u0435.<br \/>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u043e\u0439 MSIL \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043b\u043e\u0445\u043e\u0439 \u043a\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public event EventHandler Foo;  public void OnFoo() {     if (Foo != null)     {         Foo(this, EventArgs.Empty);     } }  .method public hidebysig instance void  OnFoo() cil managed {   \/\/ Code size       35 (0x23)   .maxstack  3   .locals init ([0] bool V_0)   IL_0000:  nop   IL_0001:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c this \u0432 \u0441\u0442\u0435\u043a     IL_0002:  ldfld      class [mscorlib]System.EventHandler A::Foo  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u043f\u043e\u043b\u0435 Foo   IL_0007:  ldnull  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a null   IL_0008:  cgt.un  \/\/ \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c 2 \u0432\u0435\u0440\u0445\u043d\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\u0435 (Foo \u0438 null) - \u0435\u0441\u043b\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a 0 (false)   IL_000a:  stloc.0  \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0442\u0438\u043f\u0430 bool   IL_000b:  ldloc.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0435\u0435 \u0432 \u0441\u0442\u0435\u043a   IL_000c:  brfalse.s  IL_0022  \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 false, \u0442\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a IL_0022 (return)   IL_000e:  nop   IL_000f:  ldarg.0   \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0010:  ldfld      class [mscorlib]System.EventHandler A::Foo  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u043f\u043e\u043b\u0435 Foo - !!!\u0412\u043e\u0442 \u0442\u0443\u0442 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0443\u0436\u0435 null   IL_0015:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0016:  ldsfld     class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a System.EventArgs::Empty   IL_001b:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,                                                                            class [mscorlib]System.EventArgs)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Foo(this, EventArgs.Empty)   IL_0020:  nop   IL_0021:  nop   IL_0022:  ret } \/\/ end of method A::OnFoo <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u044b \u0434\u0432\u0430\u0436\u0434\u044b \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a \u043f\u043e\u043b\u044e Foo: \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441 null (IL_0002: ldfld) \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0430 (IL_0010: ldfld). \u041c\u0435\u0436\u0434\u0443 \u0442\u0435\u043c, \u043a\u0430\u043a \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b\u0438 Foo \u043d\u0430 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e null, \u0438 \u0442\u0435\u043c, \u043a\u0430\u043a \u0437\u0430\u043d\u043e\u0432\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043a \u043d\u0435\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f, \u043f\u043e\u043b\u043e\u0436\u0438\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a \u0438 \u0432\u044b\u0437\u0432\u0430\u043b\u0438 \u043c\u0435\u0442\u043e\u0434, \u043e\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043c\u043e\u0433\u043b\u0438 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438, \u0438 \u0432\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0437 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 null \u2014 \u0437\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439, NullReferenceException.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0440\u0435\u0448\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public event EventHandler Foo;  public void OnFoo() {     EventHandler handler = Foo;     if (handler != null)     {         handler(this, EventArgs.Empty);     }    }  .method public hidebysig instance void  OnFoo() cil managed {   \/\/ Code size       32 (0x20)   .maxstack  3   .locals init ([0] class [mscorlib]System.EventHandler 'handler',            [1] bool V_1)   IL_0000:  nop   IL_0001:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c this \u0432 \u0441\u0442\u0435\u043a                           IL_0002:  ldfld      class [mscorlib]System.EventHandler A::Foo  \/\/\u0438\u0449\u0435\u043c \u043f\u043e\u043b\u0435 Foo, \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d\u043e \u043d\u0430\u0432\u0435\u0440\u0445\u0443 \u0441\u0442\u0435\u043a\u0430   IL_0007:  stloc.0  \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c Foo \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e handler   IL_0008:  ldloc.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a handler   IL_0009:  ldnull  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a null   IL_000a:  cgt.un  \/\/ \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c 2 \u0432\u0435\u0440\u0445\u043d\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\u0435 (handler \u0438 null) - \u0435\u0441\u043b\u0438 \u0440\u0430\u0432\u043d\u044b, \u0442\u043e \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a 0 (false)   IL_000c:  stloc.1  \/\/ \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0442\u0438\u043f\u0430 bool   IL_000d:  ldloc.1 \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0435\u0435 \u0432 \u0441\u0442\u0435\u043a   IL_000e:  brfalse.s  IL_001f \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 false, \u0442\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a IL_001f (return)   IL_0010:  nop   IL_0011:  ldloc.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a handler   IL_0012:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0013:  ldsfld     class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a System.EventArgs::Empty   IL_0018:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,                                                                            class [mscorlib]System.EventArgs) \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c handler(this, EventArgs.Empty)   IL_001d:  nop   IL_001e:  nop   IL_001f:  ret } \/\/ end of method A::OnFoo <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e: \u0434\u043e\u0441\u0442\u0443\u043f \u043a Foo \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 (IL_0002: ldfld), \u043f\u043e\u0442\u043e\u043c \u0432\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0438\u0434\u0435\u0442 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 handler, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c NullReferenceException \u043d\u0435\u0442.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <b>?.<\/b>.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">C# 6<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public event EventHandler Foo; public void OnFoo() {     Foo?.Invoke(this, EventArgs.Empty); }  .method public hidebysig instance void  OnFoo() cil managed {   \/\/ Code size       26 (0x1a)   .maxstack  8   IL_0000:  nop   IL_0001:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0002:  ldfld      class [mscorlib]System.EventHandler A::Foo  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u043f\u043e\u043b\u0435 Foo   IL_0007:  dup  \/\/ \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0432 \u0441\u0442\u0435\u043a\u0435 Foo   IL_0008:  brtrue.s   IL_000d  \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 true \u0438\u043b\u0438 \u043d\u0435 null \u0438 \u043d\u0435 0, \u0442\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a IL_000d (\u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430)   IL_000a:  pop  \/\/ \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0441\u0442\u0435\u043a - \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e Foo (\u043c\u044b \u043f\u043e\u043f\u0430\u043b\u0438 \u0441\u044e\u0434\u0430, \u0435\u0441\u043b\u0438 Foo == null)   IL_000b:  br.s       IL_0019  \/\/ \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430   IL_000d:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this (\u043c\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u0441\u044e\u0434\u0430, \u0435\u0441\u043b\u0438 Foo != null)   IL_000e:  ldsfld     class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a EventArgs::Empty   IL_0013:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,                                                                            class [mscorlib]System.EventArgs)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Invoke   IL_0018:  nop   IL_0019:  ret } \/\/ end of method A::OnFoo <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 C# 6 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <b>?.<\/b> \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u041c\u044b \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u043f\u043e\u043b\u0435 Foo, \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e (IL_0007: dup \u2014 \u0432\u0441\u044f \u043c\u0430\u0433\u0438\u044f \u0442\u0443\u0442), \u043f\u043e\u0442\u043e\u043c \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u043d\u0435 null \u2014 \u0442\u043e \u0438\u0434\u0435\u043c \u043a IL_000d \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 Invoke. \u0415\u0441\u043b\u0438 \u0436\u0435 Foo == null, \u0442\u043e \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0441\u0442\u0435\u043a \u0438 \u0432\u044b\u0445\u043e\u0434\u0438\u043c (IL_000b: br.s IL_0019). \u041c\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c Foo, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 NullReferenceException \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <b>?.<\/b> \u0438 Interlocked.CompareExchange.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Interlocked.CompareExchange<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public event EventHandler Foo; public void OnFoo() {     Interlocked.CompareExchange(ref Foo, null, null)?.Invoke(this, EventArgs.Empty); }  .method public hidebysig instance void  OnFoo() cil managed {   \/\/ Code size       33 (0x21)   .maxstack  8   IL_0000:  nop   IL_0001:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0002:  ldflda     class [mscorlib]System.EventHandler A::Foo  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u0430\u0434\u0440\u0435\u0441 \u043f\u043e\u043b\u044f Foo   IL_0007:  ldnull  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a null   IL_0008:  ldnull  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a null   IL_0009:  call       !!0 [mscorlib]System.Threading.Interlocked::CompareExchange&lt;class [mscorlib]System.EventHandler&gt;(!!0&,                                                                                                                         !!0,                                                                                                                         !!0)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Interlocked::CompareExchange   IL_000e:  dup  \/\/ \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0432 \u0441\u0442\u0435\u043a\u0435 Foo - \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 Interlocked::CompareExchange   IL_000f:  brtrue.s   IL_0014  \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 true \u0438\u043b\u0438 \u043d\u0435 null \u0438 \u043d\u0435 0, \u0442\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a IL_0014 (\u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430)   IL_0011:  pop  \/\/ \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0441\u0442\u0435\u043a - \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e Foo (\u043c\u044b \u043f\u043e\u043f\u0430\u043b\u0438 \u0441\u044e\u0434\u0430, \u0435\u0441\u043b\u0438 Foo == null)   IL_0012:  br.s       IL_0020  \/\/ \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430   IL_0014:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0015:  ldsfld     class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a EventArgs::Empty   IL_001a:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,                                                                            class [mscorlib]System.EventArgs)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Invoke   IL_001f:  nop   IL_0020:  ret } \/\/ end of method A::OnFoo <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u043e\u0432\u043e\u043c Interlocked.CompareExchange (IL_0009: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange), \u043f\u043e\u0442\u043e\u043c \u043a\u043e\u0434 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 (\u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 IL_000e).<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <b>?.<\/b> \u0438 Volatile.Read.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Volatile.Read<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public event EventHandler Foo; public void OnFoo() {     Volatile.Read(ref Foo)?.Invoke(this, EventArgs.Empty); }  .method public hidebysig instance void  OnFoo() cil managed {   \/\/ Code size       31 (0x1f)   .maxstack  8   IL_0000:  nop   IL_0001:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0002:  ldflda     class [mscorlib]System.EventHandler A::Foo  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a \u0430\u0434\u0440\u0435\u0441 \u043f\u043e\u043b\u044f Foo   IL_0007:  call       !!0 [mscorlib]System.Threading.Volatile::Read&lt;class [mscorlib]System.EventHandler&gt;(!!0&)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Volatile::Read   IL_000c:  dup  \/\/ \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u0435\u043c \u0432 \u0441\u0442\u0435\u043a\u0435 Foo - \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0447\u0435\u0440\u0435\u0437 Volatile::Read   IL_000d:  brtrue.s   IL_0012  \/\/ \u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0435\u043a\u0435 \u043b\u0435\u0436\u0438\u0442 true \u0438\u043b\u0438 \u043d\u0435 null \u0438 \u043d\u0435 0, \u0442\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a IL_0012 (\u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430)   IL_000f:  pop  \/\/ \u043e\u0447\u0438\u0449\u0430\u0435\u043c \u0441\u0442\u0435\u043a - \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e Foo (\u043c\u044b \u043f\u043e\u043f\u0430\u043b\u0438 \u0441\u044e\u0434\u0430, \u0435\u0441\u043b\u0438 Foo == null)   IL_0010:  br.s       IL_001e  \/\/ \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430   IL_0012:  ldarg.0  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a this   IL_0013:  ldsfld     class [mscorlib]System.EventArgs [mscorlib]System.EventArgs::Empty  \/\/ \u043a\u043b\u0430\u0434\u0435\u043c \u0432 \u0441\u0442\u0435\u043a EventArgs::Empty   IL_0018:  callvirt   instance void [mscorlib]System.EventHandler::Invoke(object,                                                                            class [mscorlib]System.EventArgs)  \/\/ \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Invoke   IL_001d:  nop   IL_001e:  ret } \/\/ end of method A::OnFoo <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0437\u043e\u0432 Interlocked.CompareExchange \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0437\u043e\u0432 Volatile.Read, \u0430 \u043f\u043e\u0442\u043e\u043c (\u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 IL_000c: dup) \u0432\u0441\u0435 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n<p>  \u0412\u0441\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <b>?.<\/b> \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u043e\u043b\u044e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0434\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u043a\u043e\u043f\u0438\u044f (MSIL \u043a\u043e\u043c\u0430\u043d\u0434\u0430 dup), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c Invoke \u0434\u043b\u044f \u0442\u043e\u0447\u043d\u043e\u0439 \u043a\u043e\u043f\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043b\u0438 \u0441 null \u2014 NullReferenceException \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442. \u0412 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u043d\u0438 \u043f\u043e\u0434\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0414\u0430, C# 6 \u0440\u0443\u043b\u0438\u0442 \u2014 \u0438 \u043d\u0435 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437. \u0418 \u043d\u0430\u043c \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f!       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/272571\/\"> http:\/\/habrahabr.ru\/post\/272571\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<br \/>\n<h2>\u041e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430<\/h2>\n<p>  \u0427\u0430\u0441\u0442\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u044e\u0442, \u0437\u0430\u0447\u0435\u043c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0430 \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u0434 \u0440\u0435\u0432\u044c\u044e, \u0434\u0430\u0436\u0435 \u043e\u043f\u044b\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442 \u043e\u0431 \u044d\u0442\u043e\u043c. \u0412 C# 6 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u044f\u0437\u044b\u043a\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0430\u0445\u0430\u0440\u0430, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 null-conditional operator (null-\u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u0438\u043b\u0438 \u042d\u043b\u0432\u0438\u0441-\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u2014 <b>?.<\/b>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0433\u043e (\u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434) \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043e\u0442 \u0414\u0436\u043e\u043d\u0430 \u0421\u043a\u0438\u0442\u0430 \u2014 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0434\u043e\u0442 \u043d\u0435\u0442 \u0433\u0443\u0440\u0443.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-269793","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269793"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/269793\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}