{"id":265853,"date":"2015-09-17T15:17:02","date_gmt":"2015-09-17T11:17:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=265853"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=265853","title":{"rendered":"Functional C#: Non-nullable reference types (\u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b)"},"content":{"rendered":"<p>       \u0422\u0440\u0435\u0442\u044c\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0432 \u0441\u0435\u0440\u0438\u0438 \u00ab\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 C#\u00bb.<\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266937\/\">Functional C#: Primitive obsession<\/a>  <\/li>\n<li><b>Functional C#: Non-nullable reference types<\/b>  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2>\u041d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432 C# \u2014 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u0435\u043b<\/h2>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code>Customer customer = _repository.GetById(id); Console.WriteLine(customer.Name); <\/code><\/pre>\n<p>  \u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0441\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u043e, \u043d\u0435 \u0442\u0430\u043a \u043b\u0438? \u041a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435?<\/p>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0434\u0435\u0441\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043c\u043e\u0436\u0435\u0442 \u0438\u043b\u0438 \u043d\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 GetById \u0432\u0435\u0440\u043d\u0443\u0442\u044c null. \u0415\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 null \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e id, \u043c\u044b \u0440\u0438\u0441\u043a\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c NullReferenceException \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435. \u0414\u0430\u0436\u0435 \u0445\u0443\u0436\u0435, \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043c, \u043a\u0430\u043a customer-\u0443 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d null, \u0438 \u0442\u0435\u043c, \u043a\u0430\u043a \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0439\u0442\u0438 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0441\u043b\u043e\u0436\u043d\u043e \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c, \u0442.\u043a. \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0433\u0434\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u0431\u044b\u043b \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d null.<\/p>\n<p>  \u0427\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0444\u0438\u0434\u0431\u0435\u043a, \u0442\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0444\u0438\u043a\u0441\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432 \u043a\u043e\u0434\u0435. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0444\u0438\u0434\u0431\u0435\u043a \u043c\u043e\u0433 \u0431\u044b \u0434\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440. \u041a\u0430\u043a \u0437\u0434\u043e\u0440\u043e\u0432\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0438 \u0434\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430 \u043d\u0430\u0441?<\/p>\n<pre><code>Customer! customer = _repository.GetById(id); Console.WriteLine(customer.Name); <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0442\u0438\u043f Customer! \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0442\u0438\u043f, \u0442.\u0435. \u0442\u0438\u043f, \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c null \u043d\u0438 \u043f\u0440\u0438 \u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430\u0445. \u0418\u043b\u0438 \u0435\u0449\u0435 \u043b\u0443\u0447\u0448\u0435:<\/p>\n<pre><code>Customer customer = _repository.GetById(id); Console.WriteLine(customer.Name); <\/code><\/pre>\n<p>  \u0422.\u0435. \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u043c\u0438 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (\u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043a\u0430\u043a value-\u0442\u0438\u043f\u044b \u0441\u0435\u0439\u0447\u0430\u0441) \u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0442\u0438\u043f, \u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u044f\u0432\u043d\u043e, \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code>Customer? customer = _repository.GetById(id); Console.WriteLine(customer.Name); <\/code><\/pre>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432 C# \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u044f\u0437\u044b\u043a\u0430. \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u0438\u043d\u0430\u0447\u0435 \u043e\u043d\u0438 \u043b\u043e\u043c\u0430\u044e\u0442 \u043f\u043e\u0447\u0442\u0438 \u0432\u0435\u0441\u044c \u0438\u043c\u0435\u044e\u0449\u0438\u0439\u0441\u044f \u043a\u043e\u0434. \u0421\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443: <a href=\"http:\/\/blog.coverity.com\/2013\/11\/20\/c-non-nullable-reference-types\/\">\u0440\u0430\u0437<\/a>, <a href=\"http:\/\/twistedoakstudios.com\/blog\/Post330_non-nullable-types-vs-c-fixing-the-billion-dollar-mistake\">\u0434\u0432\u0430<\/a>. \u0412 \u043d\u043e\u0432\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 C# \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u0435 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0443\u0442 <a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/5032\">\u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 warning-\u043e\u0432<\/a>, \u043d\u043e \u0441 \u044d\u0442\u0438\u043c \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043a\u0430 \u0442\u043e\u0436\u0435 \u043d\u0435 \u0432\u0441\u0435 \u0433\u043b\u0430\u0434\u043a\u043e.<\/p>\n<p>  \u0418 \u0445\u043e\u0442\u044f \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u044f\u0432\u043b\u044f\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c null, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e workaround-\u0430. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 Customer, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u044b \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/266937\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e<\/a>:<\/p>\n<pre><code>public class Customer {     public CustomerName Name { get; private set; }     public Email Email { get; private set; }       public Customer(CustomerName name, Email email)     {         if (name == null)             throw new ArgumentNullException(\u201cname\u201d);         if (email == null)             throw new ArgumentNullException(\u201cemail\u201d);           Name = name;         Email = email;     }       public void ChangeName(CustomerName name)     {         if (name == null)             throw new ArgumentNullException(\u201cname\u201d);           Name = name;     }       public void ChangeEmail(Email email)     {         if (email == null)             throw new ArgumentNullException(\u201cemail\u201d);           Email = email;     } } <\/code><\/pre>\n<p>  \u041c\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u043b\u0438 \u0432\u0441\u044e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u0438\u043c\u0435\u0439\u043b\u0430\u043c\u0438 \u0438 \u043a\u0430\u0441\u0442\u043e\u043c\u0435\u0440\u0430\u043c\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043d\u043e \u043c\u044b \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430\u043c\u0438 \u043d\u0430 \u043d\u0430\u043b. \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c, \u044d\u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<h2>\u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null<\/h2>\n<p>  \u0418\u0442\u0430\u043a, \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0438\u0445?<\/p>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e IL rewriter-\u0430. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c NuGet \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/github.com\/Fody\/NullGuard\">NullGuard.Fody<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0446\u0435\u043b\u0438: \u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u0432 \u0432\u0430\u0448 \u043a\u043e\u0434, \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u0432\u0430\u0448\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u0438\u0434\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 null \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043b\u0438\u0431\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043f\u0430\u043a\u0435\u0442 NullGuard.Fody \u0438 \u043f\u043e\u043c\u0435\u0442\u044c\u0442\u0435 \u0441\u0432\u043e\u044e \u0441\u0431\u043e\u0440\u043a\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c <\/p>\n<pre><code>[assembly: NullGuard(ValidationFlags.All)] <\/code><\/pre>\n<p>  \u0421 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0441\u0431\u043e\u0440\u043a\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u043d\u0430 null \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0438 \u0432\u044b\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. \u041d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441 Customer \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>public class Customer {     public CustomerName Name { get; private set; }     public Email Email { get; private set; }       public Customer(CustomerName name, Email email)     {         Name = name;         Email = email;     }       public void ChangeName(CustomerName name)     {         Name = name;     }       public void ChangeEmail(Email email)     {         Email = email;     } } <\/code><\/pre>\n<p>  \u0418 \u0434\u0430\u0436\u0435 \u0435\u0449\u0435 \u043f\u0440\u043e\u0449\u0435:<\/p>\n<pre><code>public class Customer {     public CustomerName Name { get; set; }     public Email Email { get; set; }       public Customer(CustomerName name, Email email)     {         Name = name;         Email = email;     } } <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f IL rewriter-\u0443:<\/p>\n<pre><code>public class Customer {     private CustomerName _name;     public CustomerName Name     {         get         {             CustomerName customerName = _name;               if (customerName == null)                 throw new InvalidOperationException();               return customerName;         }         set         {             if (value == null)                 throw new ArgumentNullException();               _name = value;         }     }       private Email _email;     public Email Email     {         get         {             Email email = _email;               if (email == null)                 throw new InvalidOperationException();               return email;         }         set         {             if (value == null)                 throw new ArgumentNullException();               _email = value;         }     }       public Customer(CustomerName name, Email email)     {         if (name == null)             throw new ArgumentNullException(\u201cname\u201d, \u201c[NullGuard] name is null.\u201d);         if (email == null)             throw new ArgumentNullException(\u201cemail\u201d, \u201c[NullGuard] email is null.\u201d);           Name = name;         Email = email;     } } <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c, \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u044b \u043f\u0438\u0441\u0430\u043b\u0438 \u0432\u0440\u0443\u0447\u043d\u0443\u044e, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0442\u0443\u0442 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0442\u043e\u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e.<\/p>\n<h2>\u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u0431\u044b\u0442\u044c \u0441 null?<\/h2>\n<p>  \u041a\u0430\u043a \u0431\u044b\u0442\u044c \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c null? \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 Maybe:<\/p>\n<pre><code>public struct Maybe&lt;T&gt; {     private readonly T _value;       public T Value     {         get         {             Contracts.Require(HasValue);               return _value;         }     }       public bool HasValue     {         get { return _value != null; }     }       public bool HasNoValue     {         get { return !HasValue; }     }       private Maybe([AllowNull] T value)     {         _value = value;     }       public static implicit operator Maybe&lt;T&gt;([AllowNull] T value)     {         return new Maybe&lt;T&gt;(value);     } } <\/code><\/pre>\n<p>  \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 Maybe \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c AllowNull. \u042d\u0442\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 rewriter-\u0443, \u0447\u0442\u043e \u043e\u043d \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Maybe, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code>Maybe&lt;Customer&gt; customer = _repository.GetById(id); <\/code><\/pre>\n<p>  \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u043a\u043e\u0434\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 GetById \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c null. \u041d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0443.<\/p>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0443\u0442\u0430\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0442\u0438\u043f \u0441 \u043d\u0435\u043d\u0443\u043b\u0435\u0432\u044b\u043c, \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043e\u0448\u0438\u0431\u043a\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<\/p>\n<pre><code>Maybe&lt;Customer&gt; customer = _repository.GetById(id); ProcessCustomer(customer); \/\/ Compiler error  private void ProcessCustomer(Customer customer) {     \/\/ Method body } <\/code><\/pre>\n<p>  \u0411\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, \u043d\u0435 \u0432\u0441\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e rewriter-\u0430. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432 \u0441\u0431\u043e\u0440\u043a\u0435 \u0441 WFP \u043f\u043e\u0436\u0430\u043b\u0443\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435 \u043b\u0443\u0447\u0448\u0435\u0439 \u0438\u0434\u0435\u0435\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0432 \u043d\u0435\u0439 nullable \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u0438\u0440\u043e\u0434\u0435. \u0412 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0441\u043c\u044b\u0441\u043b, \u0442.\u043a. \u0432\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e\u043c \u0438\u0437 \u044d\u0442\u0438\u0445 \u043d\u0430\u043b\u043b\u043e\u0432.<\/p>\n<p>  \u0427\u0442\u043e \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0445 \u0441\u0431\u043e\u0440\u043e\u043a, \u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e \u0441\u0442\u043e\u0438\u0442 \u0443\u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0432\u044b\u0433\u043e\u0434\u0443 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\u041e\u043d \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0433\u043e\u0432 \u043f\u0443\u0442\u0435\u043c \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0444\u0438\u0434\u0431\u0435\u043a\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 null \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u043f\u0440\u043e\u0442\u0438\u0441\u043d\u0443\u043b\u0441\u044f \u0442\u0430\u043c, \u0433\u0434\u0435 \u0435\u0433\u043e \u043d\u0435 \u0436\u0434\u0430\u043b\u0438.  <\/li>\n<li>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430. \u041d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0443\u0433\u043b\u0443\u0431\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u043c\u0435\u0442\u043e\u0434 \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u043e\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c null.  <\/li>\n<li>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u043a\u043e\u0434\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u041c\u0435\u0442\u043e\u0434\u044b \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0437\u0430\u0449\u0438\u0449\u0435\u043d\u044b \u043e\u0442 \u043d\u0443\u043b\u0435\u0432\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u043d\u0430 \u043d\u0430\u043b.  <\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435<\/h2>\n<p>  <\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266937\/\">Functional C#: Primitive obsession<\/a>  <\/li>\n<li><b>Functional C#: Non-nullable reference types<\/b>  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\n<p>  \u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438: <a href=\"http:\/\/enterprisecraftsmanship.com\/2015\/03\/13\/functional-c-non-nullable-reference-types\/\">Functional C#: Primitive obsession<\/a>               <\/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\/267063\/\"> http:\/\/habrahabr.ru\/post\/267063\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0422\u0440\u0435\u0442\u044c\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0432 \u0441\u0435\u0440\u0438\u0438 \u00ab\u0424\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 C#\u00bb.<\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266937\/\">Functional C#: Primitive obsession<\/a>  <\/li>\n<li><b>Functional C#: Non-nullable reference types<\/b>  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\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-265853","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265853","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=265853"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265853\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=265853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=265853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=265853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}