{"id":473347,"date":"2025-09-02T16:14:47","date_gmt":"2025-09-02T16:14:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=473347"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=473347","title":{"rendered":"<span>\u0422\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 C#, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0440\u0438 \u0441\u0430\u043c\u044b\u0435 \u043a\u043e\u0432\u0430\u0440\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043f\u0440\u043e\u0441\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0432 \u043f\u0440\u043e\u0434\u043e\u0432\u044b\u0435 C#-\u043f\u0440\u043e\u0435\u043a\u0442\u044b. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u043e\u043d\u0438 \u0440\u043e\u0436\u0434\u0430\u044e\u0442\u0441\u044f, \u043f\u043e\u0447\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0437\u0430\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21161<\/h3>\n<p>\u0418\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 <strong>Nullable Reference Types<\/strong> \u0438 \u0442\u0438\u0445\u0438\u0435 <code>NullReferenceException<\/code><\/p>\n<p>\u0421 \u0432\u044b\u0445\u043e\u0434\u0430 C# 8 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0443\u043c\u0435\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0442\u044c \u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c <code>null<\/code> \u0434\u0435\u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441\u0435, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 <code>#nullable enable<\/code> \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u0442\u0435 \u043d\u0430 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f. \u0422\u0430\u043c, \u0433\u0434\u0435 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u0441 <code>#nullable disable<\/code>, \u043f\u043e\u043b\u0435 \u0438\u043b\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>null<\/code>, \u0438 \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 <code>NullReferenceException<\/code>. \u042d\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0432\u044b\u0445 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u00ab\u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438, \u043d\u043e \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u043e\u0432\u00bb, \u0438 \u0442\u0430\u043a \u0436\u0438\u0432\u0443\u0442 \u043c\u0435\u0441\u044f\u0446\u0430\u043c\u0438. <\/p>\n<p>\u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0430\u043d\u0442\u0438-\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">#nullable disable public sealed class ReportService {     private readonly IEmailClient _client;      public ReportService(IEmailClient client)     {         _client = client;     }      public void SendSummary(string recipient)     {         \/\/ _client \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c null, \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u0430\u043b\u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 DI-\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440         _client.Send(recipient, BuildBody());     } }<\/code><\/pre>\n<h4>\u041a\u0430\u043a \u0444\u0438\u043a\u0441\u0438\u0442\u044c<\/h4>\n<p><strong>\u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c nullable-\u0430\u043d\u0430\u043b\u0438\u0437 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0432\u0441\u0435\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432.<\/strong><\/p>\n<p>\u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>csproj<\/code>. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c:<\/p>\n<pre><code class=\"xml\">&lt;PropertyGroup&gt;     &lt;Nullable&gt;enable&lt;\/Nullable&gt;     &lt;WarningsAsErrors&gt;nullable&lt;\/WarningsAsErrors&gt; &lt;\/PropertyGroup&gt;<\/code><\/pre>\n<p>\u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 <code>null<\/code>, \u043d\u043e \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u0438 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044e. \u0422.\u0435 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0435 \u0441\u043e\u0431\u0435\u0440\u0451\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0433\u0434\u0435-\u0442\u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u0442\u044c <code>NullReferenceException<\/code>.<\/p>\n<p><strong>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u043a\u0443 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0439<\/strong><\/p>\n<p>\u041a\u0430\u0436\u0434\u043e\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043d\u0435 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f, \u0430 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b, \u0447\u0442\u043e \u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <code>null<\/code>, \u043b\u0438\u0431\u043e \u0432\u044b \u043d\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043f\u043e\u043b\u0435 \u043a\u0430\u043a \u043d\u0430\u0434\u043e. \u0412\u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p><code>CS8600<\/code>, <code>CS8602<\/code> \u2014 \u0432\u044b \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0435 \u0438\u043b\u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e <code>null<\/code>-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p><code>CS8618<\/code> \u2014 \u043f\u043e\u043b\u0435 <code>string Name { get; set; }<\/code> \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0431\u0435\u0437 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0445\u043e\u0442\u044f <code>nullable<\/code>-\u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u043a\u043b\u044e\u0447\u0451\u043d.<\/p>\n<\/li>\n<li>\n<p><code>CS8625<\/code> \u2014 \u0432 \u043c\u0435\u0442\u043e\u0434, \u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 <code>null<\/code>, \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f <code>null<\/code>, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u044f\u0432\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0423\u0447\u0438\u043c\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c<\/strong><\/p>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u044b \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <code>null<\/code>, \u043d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u043e\u0433\u043e. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u0434\u043e \u0434\u0430\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><code>ArgumentNullException.ThrowIfNull(arg);<\/code> \u2014 \u0440\u0430\u043d\u043d\u044f\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435, \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0448\u0443\u043c.<\/p>\n<\/li>\n<li>\n<p><code>MemberNotNull<\/code> \u0438 <code>MemberNotNullWhen<\/code> \u2014 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442: \u00ab\u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u043b\u0435 \u0442\u043e\u0447\u043d\u043e \u043d\u0435 null\u00bb.<\/p>\n<\/li>\n<li>\n<p><code>??<\/code> (null-coalescing operator) \u2014 \u043a\u0440\u0430\u0442\u043a\u0430\u044f \u0437\u0430\u043c\u0435\u043d\u0430 <code>if (x == null) x = default<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u2014 <\/strong><code><strong>struct<\/strong><\/code><strong> \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b<\/strong><\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>struct<\/code> \u043f\u043e\u043b\u044f \u0441\u043e \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 <code>MyStruct { public string? Name; }<\/code>, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 <code>Name<\/code> \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f <code>null<\/code>. \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0443\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0438 unit-\u0442\u0435\u0441\u0442\u044b: \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439. \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u2014 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435: \u0435\u0441\u043b\u0438 <code>new string[10]<\/code>, \u0442\u043e \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>null<\/code>.<\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u043e\u0434\u0438\u043c \u0447\u0435\u0440\u0442\u0443<\/strong><\/p>\n<p><code>#nullable enable<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430, \u0430 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u043a\u0430\u043a <code>try\/catch<\/code> \u0438\u043b\u0438 <code>async<\/code>. \u0412\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u0438 \u0434\u043e\u0432\u0435\u043b\u0438 \u0434\u043e <strong>\u043d\u0443\u043b\u044f<\/strong> \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u043e\u0432. \u041d\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043d\u0430 \u043f\u043e\u0442\u043e\u043c. \u0421 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0432\u044b \u0434\u0430\u0451\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 <code>NullReferenceException<\/code> \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435.<\/p>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21162<\/h3>\n<p>\u0421\u043a\u0440\u044b\u0442\u044b\u0435 \u043a\u043e\u043f\u0438\u0438 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432: struct + readonly = \u0441\u044e\u0440\u043f\u0440\u0438\u0437<\/p>\n<pre><code class=\"cs\">readonly struct Point {     public int X { get; }     public int Y { get; }     public double Len() =&gt; Math.Sqrt(X * X + Y * Y); }  readonly Point _origin = new(0, 0);  double GetLen() =&gt; _origin.Len();<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c zero-alloc. \u041d\u043e \u043d\u0435\u0442: \u0435\u0441\u043b\u0438 <code>Point<\/code> <strong>\u043d\u0435<\/strong> \u043f\u043e\u043c\u0435\u0447\u0435\u043d <code>readonly<\/code>, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 <em>defensive copy<\/em> \u043f\u0435\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u043c \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e field <code>in readonly context<\/code> \u043c\u043e\u0436\u0435\u0442 \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.<\/p>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c<\/p>\n<pre><code class=\"cs\">public class MailNotifier {     public async void SendAsync(string address, string body)     {         await _smtpClient.SendAsync(address, body);     } }<\/code><\/pre>\n<p>\u0412 IL \u0443\u0432\u0438\u0434\u0438\u0442\u0435 <code>ldfld<\/code>, \u043f\u043e\u0442\u043e\u043c <code>stloc<\/code>, \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432 \u2014 \u043b\u0438\u0448\u043d\u044f\u044f \u043a\u043e\u043f\u0438\u044f. \u041d\u0430 \u0433\u043e\u0440\u044f\u0447\u0435\u043c \u043f\u0443\u0442\u0438, \u0433\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 \u0437\u043e\u0432\u0451\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u0440\u0430\u0437, \u044d\u0442\u043e \u043f\u043e\u0442\u0435\u0440\u044f \u043a\u0435\u0448-\u043b\u0430\u0439\u043d\u0430 \u0438 \u043b\u0438\u0448\u043d\u0438\u0435 32\/64 \u0431\u0430\u0439\u0442 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041b\u0435\u0447\u0438\u043c<\/p>\n<ol>\n<li>\n<p><strong>\u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c struct <\/strong><code><strong>readonly<\/strong><\/code> \u2014 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0435 \u043a\u043e\u043f\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u0442\u0438\u043f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c, \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u0441\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u044b <code>readonly<\/code>:<\/p>\n<pre><code class=\"cs\">public readonly double Len() =&gt; Math.Sqrt(X * X + Y * Y);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 struct \u0432 \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>in<\/code>-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043d\u043e <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u0434\u043b\u044f \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"cs\">double Calc(in Matrix4x4 m) { \/* \u2026 *\/ }<\/code><\/pre>\n<p>\u0438\u043d\u0430\u0447\u0435 defensive copy \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21163<\/h3>\n<p>\u0423\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0437-\u0437\u0430 \u0437\u0430\u0431\u044b\u0442\u044b\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u043e\u043a \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 A \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043b\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 B, \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0430 A \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f, A \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u043e\u043c \u0434\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b\u0448\u0435\u043b \u0438\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 \u0434\u043e\u043b\u0433\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u044d\u0442\u043e \u0442\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0433\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u0430\u043c\u0438. <\/p>\n<pre><code class=\"cs\">public sealed class StatsWindow : Window {     private readonly Timer _timer = new(TimeSpan.FromSeconds(1).TotalMilliseconds);          public StatsWindow()     {         _timer.Elapsed += OnTick; \/\/ \u0437\u0430\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f         _timer.Start();     }      private void OnTick(object? s, ElapsedEventArgs e)         =&gt; CpuLabel.Content = CpuSensor.ReadCurrent(); }<\/code><\/pre>\n<h4>\u0422\u0440\u0438 \u0443\u0440\u043e\u0432\u043d\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/h4>\n<ol>\n<li>\n<p><code><strong>IDisposable<\/strong><\/code><strong> + <\/strong><code><strong>using<\/strong><\/code><strong>\/<\/strong><code><strong>await using<\/strong><\/code><\/p>\n<pre><code class=\"cs\">public sealed class StatsWindow : Window, IDisposable {     \/\/ \u2026     public void Dispose()     {         _timer.Elapsed -= OnTick;         _timer.Dispose();     } }<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u0430\u0431\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f<\/strong><br \/>\u0414\u043b\u044f WPF:<\/p>\n<pre><code class=\"cs\">WeakEventManager&lt;Timer, ElapsedEventArgs&gt;     .AddHandler(_timer, nameof(_timer.Elapsed), OnTick);<\/code><\/pre>\n<p>\u0421\u0431\u043e\u0440\u0449\u0438\u043a \u0441\u043c\u043e\u0436\u0435\u0442 \u0443\u0434\u0430\u043b\u0438\u0442\u044c <code>StatsWindow<\/code>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0442\u0430\u0439\u043c\u0435\u0440 \u0436\u0438\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0432\u043e\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/strong><code><strong>WeakEvent&lt;TEventArgs&gt;<\/strong><\/code><strong> \u0434\u043b\u044f \u043a\u0440\u043e\u0441\u0441-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/strong><\/p>\n<pre><code class=\"cs\">public sealed class WeakEvent&lt;T&gt; where T : EventArgs {     private readonly List&lt;WeakReference&lt;EventHandler&lt;T&gt;&gt;&gt; _handlers = new();      public void Add(EventHandler&lt;T&gt; handler) =&gt;         _handlers.Add(new WeakReference&lt;EventHandler&lt;T&gt;&gt;(handler));      public void Raise(object sender, T args)     {         for (int i = _handlers.Count - 1; i &gt;= 0; i--)         {             if (_handlers[i].TryGetTarget(out var h))                 h(sender, args);             else                 _handlers.RemoveAt(i); \/\/ \u0447\u0438\u0441\u0442\u0438\u043c \u043c\u0451\u0440\u0442\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438         }     } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0438 \u043e\u0434\u0438\u043d \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u0435\u0442 \u0441\u0431\u043e\u0440\u043a\u0435 \u043c\u0443\u0441\u043e\u0440\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0410 \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a: \u0432 Visual Studio \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c <strong>Diagnostic Tools \u2192 Memory Usage \u2192 Take Snapshot<\/strong>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c\u043f\u043e \u00abEvent Handler Leaks\u00bb \u2014 \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u043d\u043e, \u043a\u0430\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0437-\u0437\u0430 \u0432\u0438\u0441\u044f\u0449\u0438\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u043e\u043a. \u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u2014 <strong>PerfView<\/strong>, \u0433\u0434\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>!DumpHeap -type &lt;TypeName&gt;<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0436\u0438\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0442\u043e \u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0441\u044b\u043b\u043a\u0438.  <\/p>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0442\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u2014 \u043e\u0442 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e null \u0434\u043e \u043f\u043e\u0442\u0435\u0440\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043d\u0435\u0432\u0438\u0434\u0438\u043c\u044b\u0445 \u043a\u043e\u043f\u0438\u0439 struct \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u043f\u043e\u0440\u0442\u0438\u0442\u044c \u0436\u0438\u0437\u043d\u044c \u0434\u0430\u0436\u0435 \u0432 \u0437\u0440\u0435\u043b\u044b\u0445 C#\u2011\u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0415\u0441\u043b\u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c\u0438 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u043c\u0438 \u0438\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u0433\u0440\u0430\u0431\u043b\u0438 \u0438\u0437 \u043e\u043f\u044b\u0442\u0430 \u2014 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.  <\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0438\u0441\u0430\u043b\u0438 \u0442\u0435\u0441\u0442\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439\u00bb \u0438\u043b\u0438 \u043b\u043e\u0432\u0438\u043b\u0438 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0430\u0439\u043c-\u0430\u0443\u0442 \u043f\u043e\u0441\u043b\u0435 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u2014 \u0437\u043d\u0430\u0447\u0438\u0442, \u0435\u0441\u0442\u044c \u043a\u0443\u0434\u0430 \u043a\u043e\u043f\u0430\u0442\u044c. \u041c\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 .NET\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438: \u043e\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f API \u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0434\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><strong>5 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/strong> \u2014 <a href=\"https:\/\/otus.pw\/Zabv\/\" rel=\"noopener noreferrer nofollow\">\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 API \u0432 ASP.NET Core: \u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0438 \u041d\u0430\u0433\u0440\u0443\u0437\u043a\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><strong>19 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/strong> \u2014 <a href=\"https:\/\/otus.pw\/OoA5\/\" rel=\"noopener noreferrer nofollow\">\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0441 CQRS \u0438 Event Sourcing \u043d\u0430 .NET Aspire<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0443\u0440\u043e\u043a\u0430\u043c, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u0441\u0432\u043e\u0435\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 C# ASP.NET, \u043f\u0440\u043e\u0439\u0434\u044f <a href=\"https:\/\/otus.pw\/P12J6\/\" rel=\"noopener noreferrer nofollow\">\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/a><\/p>\n<p><em>\u0427\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043e \u043a\u0443\u0440\u0441\u0430\u0445 \u043f\u043e C# \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0437\u0430\u043f\u0438\u0441\u044f\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432, <\/em><a href=\"https:\/\/t.me\/otus_csharp_bot?start=habr\" rel=\"noopener noreferrer nofollow\"><em>\u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u0432 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0431\u043e\u0442.<\/em><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/932888\/\"> https:\/\/habr.com\/ru\/articles\/932888\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0440\u0438 \u0441\u0430\u043c\u044b\u0435 \u043a\u043e\u0432\u0430\u0440\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u043f\u0440\u043e\u0441\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0430\u0436\u0435 \u0432 \u043f\u0440\u043e\u0434\u043e\u0432\u044b\u0435 C#-\u043f\u0440\u043e\u0435\u043a\u0442\u044b. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f, \u043a\u0430\u043a \u043e\u043d\u0438 \u0440\u043e\u0436\u0434\u0430\u044e\u0442\u0441\u044f, \u043f\u043e\u0447\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0437\u0430\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21161<\/h3>\n<p>\u0418\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 <strong>Nullable Reference Types<\/strong> \u0438 \u0442\u0438\u0445\u0438\u0435 <code>NullReferenceException<\/code><\/p>\n<p>\u0421 \u0432\u044b\u0445\u043e\u0434\u0430 C# 8 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0443\u043c\u0435\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0442\u044c \u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u043c <code>null<\/code> \u0434\u0435\u0440\u0435\u0444\u0435\u0440\u0435\u043d\u0441\u0435, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 <code>#nullable enable<\/code> \u0438 \u0440\u0435\u0430\u0433\u0438\u0440\u0443\u0435\u0442\u0435 \u043d\u0430 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f. \u0422\u0430\u043c, \u0433\u0434\u0435 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442 \u0441 <code>#nullable disable<\/code>, \u043f\u043e\u043b\u0435 \u0438\u043b\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>null<\/code>, \u0438 \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 <code>NullReferenceException<\/code>. \u042d\u0442\u043e \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0432\u044b\u0445 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u00ab\u0432\u043a\u043b\u044e\u0447\u0438\u043b\u0438, \u043d\u043e \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u0442\u043d\u0438 \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u043e\u0432\u00bb, \u0438 \u0442\u0430\u043a \u0436\u0438\u0432\u0443\u0442 \u043c\u0435\u0441\u044f\u0446\u0430\u043c\u0438. <\/p>\n<p>\u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0430\u043d\u0442\u0438-\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">#nullable disable public sealed class ReportService {     private readonly IEmailClient _client;      public ReportService(IEmailClient client)     {         _client = client;     }      public void SendSummary(string recipient)     {         \/\/ _client \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c null, \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u0430\u043b\u0438 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 DI-\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0442\u043e\u0440         _client.Send(recipient, BuildBody());     } }<\/code><\/pre>\n<h4>\u041a\u0430\u043a \u0444\u0438\u043a\u0441\u0438\u0442\u044c<\/h4>\n<p><strong>\u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c nullable-\u0430\u043d\u0430\u043b\u0438\u0437 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0432\u0441\u0435\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432.<\/strong><\/p>\n<p>\u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>csproj<\/code>. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c:<\/p>\n<pre><code class=\"xml\">&lt;PropertyGroup&gt;     &lt;Nullable&gt;enable&lt;\/Nullable&gt;     &lt;WarningsAsErrors&gt;nullable&lt;\/WarningsAsErrors&gt; &lt;\/PropertyGroup&gt;<\/code><\/pre>\n<p>\u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 <code>null<\/code>, \u043d\u043e \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u0438 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044e. \u0422.\u0435 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0435 \u0441\u043e\u0431\u0435\u0440\u0451\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0433\u0434\u0435-\u0442\u043e \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u0442\u044c <code>NullReferenceException<\/code>.<\/p>\n<p><strong>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u0438\u043a\u0443 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0439<\/strong><\/p>\n<p>\u041a\u0430\u0436\u0434\u043e\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043d\u0435 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f, \u0430 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u0438\u0433\u043d\u0430\u043b, \u0447\u0442\u043e \u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <code>null<\/code>, \u043b\u0438\u0431\u043e \u0432\u044b \u043d\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043f\u043e\u043b\u0435 \u043a\u0430\u043a \u043d\u0430\u0434\u043e. \u0412\u043e\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435:<\/p>\n<ul>\n<li>\n<p><code>CS8600<\/code>, <code>CS8602<\/code> \u2014 \u0432\u044b \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0435 \u0438\u043b\u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0435\u0441\u044c \u043a \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e <code>null<\/code>-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p><code>CS8618<\/code> \u2014 \u043f\u043e\u043b\u0435 <code>string Name { get; set; }<\/code> \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0431\u0435\u0437 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0445\u043e\u0442\u044f <code>nullable<\/code>-\u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u043a\u043b\u044e\u0447\u0451\u043d.<\/p>\n<\/li>\n<li>\n<p><code>CS8625<\/code> \u2014 \u0432 \u043c\u0435\u0442\u043e\u0434, \u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 <code>null<\/code>, \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f <code>null<\/code>, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0435\u044f\u0432\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0423\u0447\u0438\u043c\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0431\u0449\u0430\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c<\/strong><\/p>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0432\u044b \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c <code>null<\/code>, \u043d\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442 \u044d\u0442\u043e\u0433\u043e. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u0434\u043e \u0434\u0430\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0435 \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0438:<\/p>\n<ul>\n<li>\n<p><code>ArgumentNullException.ThrowIfNull(arg);<\/code> \u2014 \u0440\u0430\u043d\u043d\u044f\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435, \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0432\u0435\u0441\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0448\u0443\u043c.<\/p>\n<\/li>\n<li>\n<p><code>MemberNotNull<\/code> \u0438 <code>MemberNotNullWhen<\/code> \u2014 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442: \u00ab\u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u043b\u0435 \u0442\u043e\u0447\u043d\u043e \u043d\u0435 null\u00bb.<\/p>\n<\/li>\n<li>\n<p><code>??<\/code> (null-coalescing operator) \u2014 \u043a\u0440\u0430\u0442\u043a\u0430\u044f \u0437\u0430\u043c\u0435\u043d\u0430 <code>if (x == null) x = default<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438 \u2014 <\/strong><code><strong>struct<\/strong><\/code><strong> \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b<\/strong><\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 <code>struct<\/code> \u043f\u043e\u043b\u044f \u0441\u043e \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 <code>MyStruct { public string? Name; }<\/code>, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 <code>Name<\/code> \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f <code>null<\/code>. \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0443\u0447\u043d\u043e\u0439 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0438 unit-\u0442\u0435\u0441\u0442\u044b: \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439. \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u2014 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435: \u0435\u0441\u043b\u0438 <code>new string[10]<\/code>, \u0442\u043e \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e <code>null<\/code>.<\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u043e\u0434\u0438\u043c \u0447\u0435\u0440\u0442\u0443<\/strong><\/p>\n<p><code>#nullable enable<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435 \u043e\u043f\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430, \u0430 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438, \u043a\u0430\u043a <code>try\/catch<\/code> \u0438\u043b\u0438 <code>async<\/code>. \u0412\u043a\u043b\u044e\u0447\u0438\u043b\u0438 \u0438 \u0434\u043e\u0432\u0435\u043b\u0438 \u0434\u043e <strong>\u043d\u0443\u043b\u044f<\/strong> \u0432\u0430\u0440\u043d\u0438\u043d\u0433\u043e\u0432. \u041d\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0439\u0442\u0435 \u043d\u0430 \u043f\u043e\u0442\u043e\u043c. \u0421 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0432\u044b \u0434\u0430\u0451\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 <code>NullReferenceException<\/code> \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435.<\/p>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21162<\/h3>\n<p>\u0421\u043a\u0440\u044b\u0442\u044b\u0435 \u043a\u043e\u043f\u0438\u0438 \u0437\u043d\u0430\u0447\u0438\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432: struct + readonly = \u0441\u044e\u0440\u043f\u0440\u0438\u0437<\/p>\n<pre><code class=\"cs\">readonly struct Point {     public int X { get; }     public int Y { get; }     public double Len() =&gt; Math.Sqrt(X * X + Y * Y); }  readonly Point _origin = new(0, 0);  double GetLen() =&gt; _origin.Len();<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c zero-alloc. \u041d\u043e \u043d\u0435\u0442: \u0435\u0441\u043b\u0438 <code>Point<\/code> <strong>\u043d\u0435<\/strong> \u043f\u043e\u043c\u0435\u0447\u0435\u043d <code>readonly<\/code>, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 <em>defensive copy<\/em> \u043f\u0435\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u043c \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e field <code>in readonly context<\/code> \u043c\u043e\u0436\u0435\u0442 \u043c\u0443\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.<\/p>\n<p>\u041a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c<\/p>\n<pre><code class=\"cs\">public class MailNotifier {     public async void SendAsync(string address, string body)     {         await _smtpClient.SendAsync(address, body);     } }<\/code><\/pre>\n<p>\u0412 IL \u0443\u0432\u0438\u0434\u0438\u0442\u0435 <code>ldfld<\/code>, \u043f\u043e\u0442\u043e\u043c <code>stloc<\/code>, \u043f\u043e\u0442\u043e\u043c \u0432\u044b\u0437\u043e\u0432 \u2014 \u043b\u0438\u0448\u043d\u044f\u044f \u043a\u043e\u043f\u0438\u044f. \u041d\u0430 \u0433\u043e\u0440\u044f\u0447\u0435\u043c \u043f\u0443\u0442\u0438, \u0433\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 \u0437\u043e\u0432\u0451\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u0440\u0430\u0437, \u044d\u0442\u043e \u043f\u043e\u0442\u0435\u0440\u044f \u043a\u0435\u0448-\u043b\u0430\u0439\u043d\u0430 \u0438 \u043b\u0438\u0448\u043d\u0438\u0435 32\/64 \u0431\u0430\u0439\u0442 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041b\u0435\u0447\u0438\u043c<\/p>\n<ol>\n<li>\n<p><strong>\u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c struct <\/strong><code><strong>readonly<\/strong><\/code> \u2014 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u0430\u0449\u0438\u0442\u043d\u044b\u0435 \u043a\u043e\u043f\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u0442\u0438\u043f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c, \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u043c \u0441\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u044b <code>readonly<\/code>:<\/p>\n<pre><code class=\"cs\">public readonly double Len() =&gt; Math.Sqrt(X * X + Y * Y);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 struct \u0432 \u043c\u0435\u0442\u043e\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>in<\/code>-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043d\u043e <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u0434\u043b\u044f \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"cs\">double Calc(in Matrix4x4 m) { \/* \u2026 *\/ }<\/code><\/pre>\n<p>\u0438\u043d\u0430\u0447\u0435 defensive copy \u0432\u0435\u0440\u043d\u0451\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<h3>\u041e\u0448\u0438\u0431\u043a\u0430 \u21163<\/h3>\n<p>\u0423\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0437-\u0437\u0430 \u0437\u0430\u0431\u044b\u0442\u044b\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u043e\u043a \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 A \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043b\u0441\u044f \u043d\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 B, \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0430 A \u0441\u0438\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443. \u0415\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f, A \u043d\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u043e\u043c \u0434\u0430\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b\u0448\u0435\u043b \u0438\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 \u0434\u043e\u043b\u0433\u043e\u0436\u0438\u0432\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u044d\u0442\u043e \u0442\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0433\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u0430\u043c\u0438. <\/p>\n<pre><code class=\"cs\">public sealed class StatsWindow : Window {     private readonly Timer _timer = new(TimeSpan.FromSeconds(1).TotalMilliseconds);          public StatsWindow()     {         _timer.Elapsed += OnTick; \/\/ \u0437\u0430\u0431\u044b\u043b\u0438 \u043e\u0442\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f         _timer.Start();     }      private void OnTick(object? s, ElapsedEventArgs e)         =&gt; CpuLabel.Content = CpuSensor.ReadCurrent(); }<\/code><\/pre>\n<h4>\u0422\u0440\u0438 \u0443\u0440\u043e\u0432\u043d\u044f \u0437\u0430\u0449\u0438\u0442\u044b<\/h4>\n<ol>\n<li>\n<p><code><strong>IDisposable<\/strong><\/code><strong> + <\/strong><code><strong>using<\/strong><\/code><strong>\/<\/strong><code><strong>await using<\/strong><\/code><\/p>\n<pre><code class=\"cs\">public sealed class StatsWindow : Window, IDisposable {     \/\/ \u2026     public void Dispose()     {         _timer.Elapsed -= OnTick;         _timer.Dispose();     } }<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u0430\u0431\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f<\/strong><br \/>\u0414\u043b\u044f WPF:<\/p>\n<pre><code class=\"cs\">WeakEventManager&lt;Timer, ElapsedEventArgs&gt;     .AddHandler(_timer, nameof(_timer.Elapsed), OnTick);<\/code><\/pre>\n<p>\u0421\u0431\u043e\u0440\u0449\u0438\u043a \u0441\u043c\u043e\u0436\u0435\u0442 \u0443\u0434\u0430\u043b\u0438\u0442\u044c <code>StatsWindow<\/code>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0442\u0430\u0439\u043c\u0435\u0440 \u0436\u0438\u0432.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0432\u043e\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/strong><code><strong>WeakEvent&lt;TEventArgs&gt;<\/strong><\/code><strong> \u0434\u043b\u044f \u043a\u0440\u043e\u0441\u0441-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a<\/strong><\/p>\n<pre><code class=\"cs\">public sealed class WeakEvent&lt;T&gt; where T : EventArgs {     private readonly List&lt;WeakReference&lt;EventHandler&lt;T&gt;&gt;&gt; _handlers = new();      public void Add(EventHandler&lt;T&gt; handler) =&gt;         _handlers.Add(new WeakReference&lt;EventHandler&lt;T&gt;&gt;(handler));      public void Raise(object sender, T args)     {         for (int i = _handlers.Count - 1; i &gt;= 0; i--)         {             if (_handlers[i].TryGetTarget(out var h))                 h(sender, args);             else                 _handlers.RemoveAt(i); \/\/ \u0447\u0438\u0441\u0442\u0438\u043c \u043c\u0451\u0440\u0442\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438         }     } }<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0438 \u043e\u0434\u0438\u043d \u0441\u043b\u0443\u0448\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u043f\u043e\u043c\u0435\u0448\u0430\u0435\u0442 \u0441\u0431\u043e\u0440\u043a\u0435 \u043c\u0443\u0441\u043e\u0440\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u0410 \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a: \u0432 Visual Studio \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c <strong>Diagnostic Tools \u2192 Memory Usage \u2192 Take Snapshot<\/strong>, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c\u043f\u043e \u00abEvent Handler Leaks\u00bb \u2014 \u0441\u0440\u0430\u0437\u0443 \u0432\u0438\u0434\u043d\u043e, \u043a\u0430\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u0437-\u0437\u0430 \u0432\u0438\u0441\u044f\u0449\u0438\u0445 \u043f\u043e\u0434\u043f\u0438\u0441\u043e\u043a. \u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u2014 <strong>PerfView<\/strong>, \u0433\u0434\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 <code>!DumpHeap -type &lt;TypeName&gt;<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0436\u0438\u0432\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0442\u043e \u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0441\u044b\u043b\u043a\u0438.  <\/p>\n<p>\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0442\u0440\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u2014 \u043e\u0442 \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e null \u0434\u043e \u043f\u043e\u0442\u0435\u0440\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043d\u0435\u0432\u0438\u0434\u0438\u043c\u044b\u0445 \u043a\u043e\u043f\u0438\u0439 struct \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e\u0442 \u043f\u043e\u0440\u0442\u0438\u0442\u044c \u0436\u0438\u0437\u043d\u044c \u0434\u0430\u0436\u0435 \u0432 \u0437\u0440\u0435\u043b\u044b\u0445 C#\u2011\u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0415\u0441\u043b\u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c\u0438 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u043c\u0438 \u0438\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u0433\u0440\u0430\u0431\u043b\u0438 \u0438\u0437 \u043e\u043f\u044b\u0442\u0430 \u2014 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.  <\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0438\u0441\u0430\u043b\u0438 \u0442\u0435\u0441\u0442\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u00ab\u043d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439\u00bb \u0438\u043b\u0438 \u043b\u043e\u0432\u0438\u043b\u0438 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0430\u0439\u043c-\u0430\u0443\u0442 \u043f\u043e\u0441\u043b\u0435 \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u2014 \u0437\u043d\u0430\u0447\u0438\u0442, \u0435\u0441\u0442\u044c \u043a\u0443\u0434\u0430 \u043a\u043e\u043f\u0430\u0442\u044c. \u041c\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b\u0438 \u0434\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430, \u0433\u0434\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 .NET\u2011\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438: \u043e\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f API \u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0434\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432:<\/p>\n<ul>\n<li>\n<p><strong>5 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/strong> \u2014 <a href=\"https:\/\/otus.pw\/Zabv\/\" rel=\"noopener noreferrer nofollow\">\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 API \u0432 ASP.NET Core: \u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0438 \u041d\u0430\u0433\u0440\u0443\u0437\u043a\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><strong>19 \u0430\u0432\u0433\u0443\u0441\u0442\u0430 \u0432 20:00<\/strong> \u2014 <a href=\"https:\/\/otus.pw\/OoA5\/\" rel=\"noopener noreferrer nofollow\">\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u0441 CQRS \u0438 Event Sourcing \u043d\u0430 .NET Aspire<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u043e \u0432\u0441\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0443\u0440\u043e\u043a\u0430\u043c, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u0441\u0432\u043e\u0435\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0437\u043d\u0430\u043d\u0438\u0439 C# ASP.NET, \u043f\u0440\u043e\u0439\u0434\u044f <a href=\"https:\/\/otus.pw\/P12J6\/\" rel=\"noopener noreferrer nofollow\">\u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<\/a><\/p>\n<p><em>\u0427\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043e \u043a\u0443\u0440\u0441\u0430\u0445 \u043f\u043e C# \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0437\u0430\u043f\u0438\u0441\u044f\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432, <\/em><a href=\"https:\/\/t.me\/otus_csharp_bot?start=habr\" rel=\"noopener noreferrer nofollow\"><em>\u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u0432 \u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u0431\u043e\u0442.<\/em><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/932888\/\"> https:\/\/habr.com\/ru\/articles\/932888\/<\/a><br \/><\/br><\/br><\/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-473347","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473347","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=473347"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473347\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=473347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=473347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=473347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}