{"id":290356,"date":"2018-10-05T11:05:29","date_gmt":"2018-10-05T07:05:29","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=290356"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=290356","title":{"rendered":"Pattern matching \u0432 C# 7"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u0412 C# 7 \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0434\u043e\u043b\u0433\u043e\u0436\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u00ab\u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c\u00bb. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a F#, \u0442\u043e \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 \u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u0441 \u0441\u043b\u0435\u0433\u043a\u0430 \u0440\u0430\u0437\u043e\u0447\u0430\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u0434\u0430\u0436\u0435 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u043e\u043d\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0432 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c!<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tw\/bk\/ck\/twbkckursaas-xb76976szvpmm0.jpeg\"><a name=\"habracut\"><\/a><\/p>\n<p>  \u041a\u0430\u0436\u0434\u0430\u044f \u043d\u043e\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u043f\u0430\u0441\u043d\u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u041d\u043e\u0432\u044b\u0435 \u0443\u0440\u043e\u0432\u043d\u0438 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0439 \u0445\u043e\u0440\u043e\u0448\u0438, \u043d\u043e \u0447\u0442\u043e\u0431\u044b \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u043a\u0430\u043a \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u0438 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0437\u0435\u0446 \u0432 C# \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 is, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432 \u0431\u043b\u043e\u043a\u0435 case \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 switch.<br \/>  \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u0432:<\/p>\n<ul>\n<li>\u043e\u0431\u0440\u0430\u0437\u0435\u0446\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b;<\/li>\n<li>\u043e\u0431\u0440\u0430\u0437\u0435\u0446\u0442\u0438\u043f\u0430;<\/li>\n<li>\u043e\u0431\u0440\u0430\u0437\u0435\u0446\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439.<\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0421\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 is<\/h2>\n<p>  <\/p>\n<pre><code class=\"cs\">public void IsExpressions(object o) {     \/\/ Alternative way checking for null     if (o is null) Console.WriteLine(\"o is null\");       \/\/ Const pattern can refer to a constant value     const double value = double.NaN;     if (o is value) Console.WriteLine(\"o is value\");       \/\/ Const pattern can use a string literal     if (o is \"o\") Console.WriteLine(\"o is \\\"o\\\"\");       \/\/ Type pattern     if (o is int n) Console.WriteLine(n);       \/\/ Type pattern and compound expressions     if (o is string s &amp;&amp; s.Trim() != string.Empty)         Console.WriteLine(\"o is not blank\"); }<\/code><\/pre>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f is \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u0438\u043f\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0437\u0446\u0430.<\/p>\n<p>  \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 is \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ul>\n<li>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c if, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u044e\u044e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438.<\/li>\n<li>\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c if, \u044f\u0432\u043d\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0437\u0435\u0446\u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442.<\/li>\n<li>\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 is \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430.<\/li>\n<\/ul>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u0441\u043b\u0443\u0447\u0430\u044f:<\/p>\n<pre><code class=\"cs\">public void ScopeAndDefiniteAssigning(object o) {     if (o is string s &amp;&amp; s.Length != 0)     {         Console.WriteLine(\"o is not empty string\");     }       \/\/ Can't use 's' any more. 's' is already declared in the current scope.     if (o is int n || (o is string s2 &amp;&amp; int.TryParse(s2, out n)))     {         Console.WriteLine(n);     } }<\/code><\/pre>\n<p>  \u041f\u0435\u0440\u0432\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 if \u0432\u0432\u043e\u0434\u0438\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e s, \u0432\u0438\u0434\u0438\u043c\u0443\u044e \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0441\u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430. \u042d\u0442\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u043e, \u043d\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442 \u043b\u043e\u0433\u0438\u043a\u0443, \u0435\u0441\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f if \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u043f\u043e\u043f\u044b\u0442\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0438\u043c\u044f. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0435 \u0438\u043c\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0432\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 is, \u044f\u0432\u043d\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 true. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f n \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 if \u043d\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u043d\u0434\u0435, \u043d\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0430 \u0443\u0436\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e out \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 int.TryParse.<\/p>\n<p>  \u0422\u0440\u0435\u0442\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0439 \u0432\u044b\u0448\u0435, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">public void BoxTwice(int n) {     if (n is 42) Console.WriteLine(\"n is 42\"); }<\/code><\/pre>\n<p>  \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 is \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 object.Equals(\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f) [\u0445\u043e\u0442\u044f \u0432 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0430\u0445 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 ==]:<\/p>\n<pre><code class=\"cs\">public void BoxTwice(int n) {     if (object.Equals(42, n))     {         Console.WriteLine(\"n is 42\");     } }<\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0432\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u00ab\u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0430-\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00bb, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043d\u0430 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0443\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0420\u0430\u043d\u044c\u0448\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 o is null \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u043e \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0443, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043e \u0438\u043c\u0435\u043b\u0430 \u0442\u0438\u043f, \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0449\u0438\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 null (\u0441\u043c. <a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/13247\">Suboptimal code for e is null<\/a> (\u00ab\u041d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f e is null\u00bb)), \u043d\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u0434\u0430, \u0447\u0442\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e (\u0432\u043e\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 <a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\/20642\">\u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 github<\/a>).<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f n \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0442\u0438\u043f\u0443 object, \u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 o is 42 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u00ab\u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0430-\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00bb (\u0434\u043b\u044f \u043b\u0438\u0442\u0435\u0440\u0430\u043b\u0430 42), \u0445\u043e\u0442\u044f \u043f\u043e\u0445\u043e\u0436\u0438\u0439 \u043a\u043e\u0434 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 switch \u043d\u0435 \u043f\u0440\u0438\u0432\u0435\u043b \u0431\u044b \u043a \u044d\u0442\u043e\u043c\u0443.<\/p>\n<h2>\u041e\u0431\u0440\u0430\u0437\u0435\u0446 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 is<\/h2>\n<p>  \u041e\u0431\u0440\u0430\u0437\u0435\u0446 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u2014 \u044d\u0442\u043e \u043e\u0441\u043e\u0431\u044b\u0439 \u0432\u0438\u0434 \u043e\u0431\u0440\u0430\u0437\u0446\u0430 \u0442\u0438\u043f\u0430 \u0441 \u043e\u0434\u043d\u0438\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c: \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e, \u0434\u0430\u0436\u0435 null.<\/p>\n<pre><code class=\"cs\">public void IsVar(object o) {     if (o is var x) Console.WriteLine($\"x: {x}\"); }<\/code><\/pre>\n<p>  \u0412\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 o is object \u043f\u0440\u0438\u043c\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 true, \u0435\u0441\u043b\u0438 o \u043d\u0435 \u0440\u0430\u0432\u043d\u0430 null, \u043e\u0434\u043d\u0430\u043a\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 o is var x \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 true. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 \u0432\u044b\u043f\u0443\u0441\u043a\u0430* \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f if \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0438\u0434\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 Console. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0430\u0435\u0442 \u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435: if (!(o is var x)) Console.WriteLine(\u00abUnreachable\u00bb). \u0415\u0441\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u0434\u0430, \u0447\u0442\u043e \u0438 \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u0438\u0441\u043f\u0440\u0430\u0432\u044f\u0442.<\/p>\n<p>  <i>* \u041d\u0435\u044f\u0441\u043d\u043e, \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 \u0432\u044b\u043f\u0443\u0441\u043a\u0430. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043a\u043e\u0440\u0435\u043d\u044c \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043e\u0434\u0438\u043d: \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430. \u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0441\u0443\u0434\u044f \u043f\u043e <a href=\"http:\/\/Pattern%20matching\">\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044e<\/a> \u041d\u0438\u043b\u0430 \u0413\u0430\u0444\u0442\u0435\u0440\u0430 (Neal Gafter), \u0441\u043a\u043e\u0440\u043e \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f: \u00ab\u041a\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d \u0441 \u043d\u0443\u043b\u044f (\u0447\u0442\u043e\u0431\u044b \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043e\u0431\u0440\u0430\u0437\u0446\u044b). \u042f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0439, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0435, \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 \u043d\u043e\u0432\u043e\u043c \u043a\u043e\u0434\u0435 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e. \u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f\u00bb.<\/i><\/p>\n<p>  \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 null \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0439 \u0438 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0430\u0441\u043d\u043e\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0435\u0442\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0437\u0446\u0430, \u0442\u043e \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"cs\">public void VarPattern(IEnumerable&lt;string&gt; s) { if (s.FirstOrDefault(o =&gt; o != null) is var v &amp;&amp; int.TryParse(v, out var n)) { Console.WriteLine(n); } }<\/code><\/pre>\n<p>  <\/p>\n<h2>\u0412\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 Is \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 Elvis<\/h2>\n<p>  \u0415\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c. \u041e\u0431\u0440\u0430\u0437\u0435\u0446\u0442\u0438\u043f\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u043e \u043d\u0435 \u0440\u0430\u0432\u043d\u043e null. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u043b\u043e\u0433\u0438\u043a\u0443 \u00ab\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438\u00bb \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c, \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u044e\u0449\u0438\u043c null, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cs\">public void WithNullPropagation(IEnumerable&lt;string&gt; s) {     if (s?.FirstOrDefault(str =&gt; str.Length &gt; 10)?.Length is int length)     {         Console.WriteLine(length);     }       \/\/ Similar to     if (s?.FirstOrDefault(str =&gt; str.Length &gt; 10)?.Length is var length2 &amp;&amp; length2 != null)     {         Console.WriteLine(length2);     }       \/\/ And similar to     var length3 = s?.FirstOrDefault(str =&gt; str.Length &gt; 10)?.Length;     if (length3 != null)     {         Console.WriteLine(length3);     } }<\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u0440\u0430\u0437\u0435\u0446\u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<h2>\u0421\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u0432 \u0431\u043b\u043e\u043a\u0430\u0445 case<\/h2>\n<p>  \u0412 C# 7 \u0431\u044b\u043b \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 switch, \u0442\u0430\u043a \u0447\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u0446\u044b \u043c\u043e\u0433\u0443\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 case:<\/p>\n<pre><code class=\"cs\">public static int Count&lt;T&gt;(this IEnumerable&lt;T&gt; e) {     switch (e)     {         case ICollection&lt;T&gt; c: return c.Count;         case IReadOnlyCollection&lt;T&gt; c: return c.Count;         \/\/ Matches concurrent collections         case IProducerConsumerCollection&lt;T&gt; pc: return pc.Count;         \/\/ Matches if e is not null         case IEnumerable&lt;T&gt; _: return e.Count();         \/\/ Default case is handled when e is null         default: return 0;     } }<\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0435\u0440\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 switch.<\/p>\n<ol>\n<li>\u0421 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c switch \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0442\u0438\u043f\u0430.<\/li>\n<li>\u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c case \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0437\u0435\u0446.<\/li>\n<li>\u0412\u0430\u0436\u0435\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439 case. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u0434\u0430\u0441\u0442 \u043e\u0448\u0438\u0431\u043a\u0443, \u0435\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u043c\u0443 \u0442\u0438\u043f\u0443, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u2014 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u043c\u0443.<\/li>\n<li>\u041d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 null**. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 case \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043d\u0435 \u0440\u0430\u0432\u0435\u043d null.<\/li>\n<\/ol>\n<p>  <i>** \u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 case \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0432 C# 7, \u2014 \u043e\u0431\u0440\u0430\u0437\u0446\u044b \u043f\u0443\u0441\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439. \u0421\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u043c\u044f _ \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043d\u0435 \u043d\u0443\u0436\u043d\u0430. \u041e\u0431\u0440\u0430\u0437\u0446\u0443 \u0442\u0438\u043f\u0430 \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 case \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u044d\u0442\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c _.<\/i><\/p>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 switch \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442\u044b:<\/p>\n<pre><code class=\"cs\">public static void FizzBuzz(object o) {     switch (o)     {         case string s when s.Contains(\"Fizz\") || s.Contains(\"Buzz\"):             Console.WriteLine(s);             break;         case int n when n % 5 == 0 &amp;&amp; n % 3 == 0:             Console.WriteLine(\"FizzBuzz\");             break;         case int n when n % 5 == 0:             Console.WriteLine(\"Fizz\");             break;         case int n when n % 3 == 0:             Console.WriteLine(\"Buzz\");             break;         case int n:             Console.WriteLine(n);             break;     } }<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 <a href=\"http:\/\/wiki.c2.com\/?FizzBuzzTest\">FizzBuzz<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442, \u0430 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0447\u0438\u0441\u043b\u043e.<\/p>\n<p>  \u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 switch \u043c\u043e\u0436\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439 case \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0442\u0438\u043f\u043e\u043c. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u0438\u043f\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"cs\">public static void FizzBuzz(object o) {     \/\/ All cases can match only if the value is not null     if (o != null)     {         if (o is string s &amp;&amp;             (s.Contains(\"Fizz\") || s.Contains(\"Buzz\")))         {             Console.WriteLine(s);             return;         }           bool isInt = o is int;         int num = isInt ? ((int)o) : 0;         if (isInt)         {             \/\/ The type check and unboxing happens only once per group             if (num % 5 == 0 &amp;&amp; num % 3 == 0)             {                 Console.WriteLine(\"FizzBuzz\");                 return;             }             if (num % 5 == 0)             {                 Console.WriteLine(\"Fizz\");                 return;             }             if (num % 3 == 0)             {                 Console.WriteLine(\"Buzz\");                 return;             }               Console.WriteLine(num);         }     } }<\/code><\/pre>\n<p>  \u041d\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0434\u0432\u0443\u0445 \u0432\u0435\u0449\u0430\u0445:<\/p>\n<p>  1. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u0438\u043f\u043e\u0432, \u0438 \u0435\u0441\u043b\u0438 \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f case \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438, \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u043c\u0435\u043d\u0435\u0435 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cs\">switch (o) {     \/\/ The generated code is less optimal:     \/\/ If o is int, then more than one type check and unboxing operation     \/\/ may happen.     case int n when n == 1: return 1;     case string s when s == \"\": return 2;     case int n when n == 2: return 3;     default: return -1; }<\/code><\/pre>\n<p>  \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  if (o is int n &amp;&amp; n == 1) return 1;  <\/p>\n<pre><code class=\"cs\">if (o is string s &amp;&amp; s == \"\") return 2; if (o is int n2 &amp;&amp; n2 == 2) return 3; return -1;<\/code><\/pre>\n<p>  2. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"cs\">switch (o) {     case int n: return 1;     \/\/ Error: The switch case has already been handled by a previous case.     case int n when n == 1: return 2; }<\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442 \u0441\u0438\u043b\u044c\u043d\u0435\u0435 \u0434\u0440\u0443\u0433\u043e\u0433\u043e, \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0437\u0430\u043c\u0435\u0449\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f case:<\/p>\n<pre><code class=\"cs\">switch (o) {     case int n when n &gt; 0: return 1;     \/\/ Will never match, but the compiler won't warn you about it     case int n when n &gt; 1: return 2; }<\/code><\/pre>\n<p>  <\/p>\n<h2>\u041a\u0440\u0430\u0442\u043a\u043e \u043e \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c<\/h2>\n<p>  <\/p>\n<ul>\n<li>\u0412 C# 7 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0431\u0440\u0430\u0437\u0446\u044b: \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0442\u0438\u043f\u0430, \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438 \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u043f\u0443\u0441\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439.<\/li>\n<li>\u041e\u0431\u0440\u0430\u0437\u0446\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 is \u0438 \u0432 \u0431\u043b\u043e\u043a\u0430\u0445 case.<\/li>\n<li>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u0440\u0430\u0437\u0446\u0430 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 is \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043b\u0435\u043a\u0430 \u043e\u0442 \u0438\u0434\u0435\u0430\u043b\u0430 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/li>\n<li>\u041e\u0431\u0440\u0430\u0437\u0446\u044b \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442, \u0441 \u043d\u0438\u043c\u0438 \u043d\u0430\u0434\u043e \u0431\u044b\u0442\u044c \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u044b\u043c\u0438.<\/li>\n<li>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 switch \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u0442\u0438\u043f\u0430 \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442\u0430\u043c\u0438 \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 when.<\/li>\n<\/ul>\n<h2>\u0421\u043e\u0431\u044b\u0442\u0438\u0435 \u043f\u043e Unity \u0432 \u041c\u043e\u0441\u043a\u0432\u0435 \u2014 Unity Moscow Meetup 2018.1<\/h2>\n<p>  11 \u043e\u043a\u0442\u044f\u0431\u0440\u044f, \u0432 \u0447\u0435\u0442\u0432\u0435\u0440\u0433, \u0432 \u0412\u0428\u0411\u0418 \u0441\u043e\u0441\u0442\u043e\u0438\u0442\u0441\u044f Unity Moscow Meetup 2018.1. \u042d\u0442\u043e \u043f\u0435\u0440\u0432\u0430\u044f \u0432 \u044d\u0442\u043e\u043c \u0441\u0435\u0437\u043e\u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430 Unity \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0432 \u041c\u043e\u0441\u043a\u0432\u0435. \u0422\u0435\u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043c\u0438\u0442\u0430\u043f\u0430 \u0431\u0443\u0434\u0435\u0442 AR\/VR. \u0412\u0430\u0441 \u0436\u0434\u0443\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0434\u043e\u043a\u043b\u0430\u0434\u044b, \u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u0430\u043c\u0438 \u0438\u043d\u0434\u0443\u0441\u0442\u0440\u0438\u0438, \u0430 \u0442\u0430\u043a \u0436\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u0435\u043c\u043e-\u0437\u043e\u043d\u0430 \u043e\u0442 MSI.<\/p>\n<p>  <a href=\"http:\/\/unimosmeet.ru\/event2018_1\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438<\/a><\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <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\/post\/423229\/\"> https:\/\/habr.com\/post\/423229\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u0412 C# 7 \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0434\u043e\u043b\u0433\u043e\u0436\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u00ab\u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c\u00bb. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u044f\u0437\u044b\u043a\u0430\u043c\u0438, \u0442\u0430\u043a\u0438\u043c\u0438 \u043a\u0430\u043a F#, \u0442\u043e \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 \u0442\u043e\u043c \u0432\u0438\u0434\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u0441 \u0441\u043b\u0435\u0433\u043a\u0430 \u0440\u0430\u0437\u043e\u0447\u0430\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u043e \u0434\u0430\u0436\u0435 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u043e\u043d\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434 \u0432 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c!<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tw\/bk\/ck\/twbkckursaas-xb76976szvpmm0.jpeg\"><\/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-290356","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290356","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=290356"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290356\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}