{"id":201996,"date":"2013-11-13T16:28:04","date_gmt":"2013-11-13T12:28:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=201996"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=201996","title":{"rendered":"<span class=\"post_title\">The art of Generics<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u2013 \u043e\u043d\u0438 \u0436\u0435 generics, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u043c\u043e\u0449\u043d\u0435\u0439\u0448\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>  CLR \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 MSIL, \u0438 \u0432\u0441\u0435\u0433\u043e \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u0441 \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0412\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 C++, \u043d\u043e \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0442\u043e \u043f\u043e \u0438\u0437\u044f\u0449\u0435\u0441\u0442\u0432\u0443 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u0443\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430 C#, \u0442\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432 \u044d\u0442\u043e\u043c.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3><font color=\"SandyBrown\">\u258c<\/font>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430\u0445<\/h3>\n<p>  \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u041e\u041e\u041f \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u0441\u0432\u044f\u0437\u043a\u0435.<\/p>\n<p>  [<i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u043d\u0438\u0436\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043a \u00ab\u0442\u0440\u044e\u043a\u0430\u043c\u00bb \u0441 generics, \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u043c\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0446\u0435\u043b\u044c\u044e \u0441\u0442\u0430\u0442\u044c\u0438. \u0410\u0432\u0442\u043e\u0440\u0443 \u043b\u0438\u0448\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0445\u043e\u0434 \u043c\u044b\u0441\u043b\u0435\u0439<\/i>.]<\/p>\n<p>  \u0427\u0435\u0433\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u043e\u0438\u0442 MVC. \u0413\u0434\u0435 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"http:\/\/en.wikipedia.org\/wiki\/Strategy_pattern\">\u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e<\/a>, \u0430 \u043b\u0443\u0447\u0448\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Factory_method_pattern\">\u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/a> (\u043d\u0435 \u043f\u0443\u0442\u0430\u0442\u044c \u0441 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Abstract_factory_pattern\">\u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0439 \u0444\u0430\u0431\u0440\u0438\u043a\u043e\u0439<\/a>).<\/p>\n<p>  \u041b\u0443\u0447\u0448\u0435, \u0447\u0435\u043c GoF, \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0432\u0438\u043d\u0435\u043c\u0441\u044f \u0434\u0430\u043b\u0435\u0435.<\/p>\n<p>  \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u0430\u043a\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u043a\u0430\u043a:  <\/p>\n<ul>\n<li>Multiple dispatch<\/li>\n<li>Double dispatch (\u043e\u043d \u0436\u0435 \u0432\u0438\u0434 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 Visitor)<\/li>\n<\/ul>\n<p>  \u0421\u0443\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 single dispatch \u2013 \u043e\u043d\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C# 4 \u0438 \u0435\u0433\u043e dynamic \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0437 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Multiple_dispatch\">wikipedia<\/a>.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Muliple dispatch<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">class Program {     class Thing { }     class Asteroid : Thing { }     class Spaceship : Thing { }      static void CollideWithImpl(Asteroid x, Asteroid y)     {         Console.WriteLine(&quot;Asteroid collides with Asteroid&quot;);     }      static void CollideWithImpl(Asteroid x, Spaceship y)     {         Console.WriteLine(&quot;Asteroid collides with Spaceship&quot;);     }      static void CollideWithImpl(Spaceship x, Asteroid y)     {         Console.WriteLine(&quot;Spaceship collides with Asteroid&quot;);     }      static void CollideWithImpl(Spaceship x, Spaceship y)     {         Console.WriteLine(&quot;Spaceship collides with Spaceship&quot;);     }      static void CollideWith(Thing x, Thing y)     {         dynamic a = x;         dynamic b = y;         CollideWithImpl(a, b);     }      static void Main(string[] args)     {         var asteroid = new Asteroid();         var spaceship = new Spaceship();         CollideWith(asteroid, spaceship);         CollideWith(spaceship, spaceship);     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0431\u044b \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430.<br \/>  \u041d\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u043a Double dispatch. \u041f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Double dispatch<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">class Program {     interface ICollidable     {         void CollideWith(ICollidable other);     }      class Asteroid : ICollidable     {         public void CollideWith(Asteroid other)         {             Console.WriteLine(&quot;Asteroid collides with Asteroid&quot;);         }          public void CollideWith(Spaceship spaceship)         {             Console.WriteLine(&quot;Asteroid collides with Spaceship&quot;);         }          public void CollideWith(ICollidable other)         {             other.CollideWith(this);         }     }      class Spaceship : ICollidable     {         public void CollideWith(ICollidable other)         {             other.CollideWith(this);         }          private void CollideWith(Asteroid asteroid)         {             Console.WriteLine(&quot;Spaceship collides with Asteroid&quot;);         }          private void CollideWith(Spaceship spaceship)         {             Console.WriteLine(&quot;Spaceship collides with Spaceship&quot;);         }     }      static void Main(string[] args)     {         var asteroid = new Asteroid();         var spaceship = new Spaceship();         asteroid.CollideWith(spaceship);         asteroid.CollideWith(asteroid);     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0427\u0442\u043e \u0436\u0435, \u043a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0438 \u0431\u0435\u0437 dynamic.<\/p>\n<p>  \u0422\u0430\u043a \u043a \u0447\u0435\u043c\u0443 \u0432\u0441\u0435 \u044d\u0442\u043e?<\/p>\n<p>  \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u043e\u0441\u0442 \u2013 \u0435\u0441\u043b\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u0440\u043d\u0443\u044e \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e (<b>single dispatch<\/b>), \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u043f\u043e \u0442\u0438\u043f\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044f \u043a \u0441\u043b\u0443\u0447\u0430\u044e \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0435 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c (<b>multiple dispatch<\/b>), \u0442\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0438 \u0441 generics?!<\/p>\n<h3><font color=\"DeepSkyBlue\">\u258c<\/font>Covariance &#038;&#038; Contravariance<\/h3>\n<p>  \u0412\u043e\u043e\u0431\u0449\u0435, \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u0442\u0438\u043f\u043e\u0432 \u0432 \u043b\u044e\u0431\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e \u0441\u043e\u0431\u043e\u0439 \u0440\u0430\u0437\u0443\u043c\u0435\u044e\u0449\u0438\u043c\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">var asteroid = new Asteroid();   ICollidable collidable = asteroid; <\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (assignment compatibility).<\/p>\n<p>  \u041a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 generics.<\/p>\n<pre><code class=\"cs\">List&lt;Asteroid&gt; asteroids = new List&lt;Asteroid&gt;();   IEnumerable&lt;ICollidable&gt; collidables = asteroids; <\/code><\/pre>\n<p>  \u0414\u0435\u043a\u043b\u0430\u0440\u0430\u0446\u0438\u044f IEnumerable \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cs\">public interface IEnumerable&lt;out T&gt; : IEnumerable {   IEnumerator&lt;T&gt; GetEnumerator(); } <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0433\u043e \u0441\u043b\u043e\u0432\u0430 out \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0442\u0438\u043f List&lt;Asteroid&gt; \u043a \u0442\u0438\u043f\u0443 IEnumerable&lt;ICollidable&gt;, \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a\u043b\u0430\u0441\u0441\u043e\u043c List&lt;T&gt;.<\/p>\n<p>  \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u0412\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0442\u0438\u043f\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u043a <i>out T<\/i> \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0434\u0430\u0436\u0435 \u0432 \u0432\u0438\u0434\u0435 \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cs\">interface ICustomInterface&lt;out T&gt; {     T Do(T target); \/\/compile-time error     T Do(IList&lt;T&gt; targets); \/\/compile-time error } <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0436\u0435, \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u044d\u0442\u0443 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0443, \u0430 \u043f\u043e\u043a\u0430 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043d\u0430\u0448\u0435\u0439 \u0446\u0435\u043b\u0438 \u2013 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e generics.<\/p>\n<h3><font color=\"limeGreen\">\u258c<\/font>Generics compile-time checking<\/h3>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n<pre><code class=\"cs\">public interface IReader&lt;T&gt; {     T Read(T[] arr, int index); } <\/code><\/pre>\n<p>  \u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435\u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043b\u0438\u0448\u044c \u0434\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440, \u0447\u0438\u0441\u0435\u043b \u0438\u043b\u0438 \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439? \u0422.\u0435. \u0432\u0432\u0435\u0441\u0442\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0442\u0438\u043f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438?<\/p>\n<p>  C# \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0430\u043a\u0443\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u041c\u043e\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u0430\u043a struct, class \u0438\u043b\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0442\u0438\u043f (\u0435\u0449\u0435 \u0435\u0441\u0442\u044c new()) \u0434\u043b\u044f \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430.<\/p>\n<pre><code class=\"cs\">public interface IReader&lt;T&gt; where T : class {     T Read(T[] arr, int index); } <\/code><\/pre>\n<p>  \u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0430\u0441\u0442\u0435\u0440\u043e\u0438\u0434\u0430\u043c\u0438 \u0434\u043b\u044f multiple dispatch?<\/p>\n<p>  \u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0435 \u0436\u0435 \u043c\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0434\u043b\u044f \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 IReader.<\/p>\n<pre><code class=\"cs\">public class SignedIntegersReader : IReader&lt;Int32&gt;, IReader&lt;Int16&gt;, IReader&lt;Int64&gt; {     int IReader&lt;int&gt;.Read(int[] arr, int index)     {         return arr[index];     }      short IReader&lt;short&gt;.Read(short[] arr, int index)     {         return arr[index];     }      long IReader&lt;long&gt;.Read(long[] arr, int index)     {         return arr[index];     } } <\/code><\/pre>\n<p>  \u0414\u0443\u043c\u0430\u044e, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u2013 \u043f\u043e\u0447\u0435\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u044f\u0432\u043d\u0430\u044f (explicit) \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430?<\/p>\n<p>  \u0412\u0441\u0435 \u0434\u0435\u043b\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0435 \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430.<\/p>\n<p>  \u0422\u0430\u043a, \u043a\u043e\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0441 \u0442\u0438\u043f\u043e\u043c T, \u0434\u0430\u0436\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, IList.<\/p>\n<p>  \u0410 \u0442\u0430\u043a \u043a\u0430\u043a \u0432 C# \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043f\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u043c\u0443 \u0442\u0438\u043f\u0443 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043d\u0435\u044f\u0432\u043d\u0430\u044f \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0441 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438, \u0433\u0434\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0440\u0430\u0432\u043d\u043e \u043d\u0443\u043b\u044e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u0427\u0442\u043e \u0436\u0435, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435.<\/p>\n<pre><code class=\"cs\">public static class ReaderExtensions {     public static T Read&lt;TReader, T&gt;(this TReader reader, T[] arr, int index)                                                              where TReader : IReader&lt;T&gt;     {         return reader.Read(arr, index);     } }  class Program {     static void Main(string[] args)     {         var reader = new SignedIntegersReader();          var arr = new int[] {128, 256};          for (int i = 0; i &lt; arr.Length; i++)         {             Console.WriteLine(&quot;Reader result: {0}&quot;, reader.Read(arr, i));         }     } } <\/code><\/pre>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 arr \u043d\u0430 float[].<\/p>\n<pre><code class=\"cs\">class Program {     static void Main(string[] args)     {         var reader = new SignedIntegersReader();          var arr = new float[] {128.0f, 256.0f};          for (int i = 0; i &lt; arr.Length; i++)         {             Console.WriteLine(&quot;Reader result: {0}&quot;, reader.Read(arr, i)); \/\/compile-time error         }     } } <\/code><\/pre>\n<p>  \u041d\u043e \u044d\u0442\u043e \u0436\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f?! \u041a\u0430\u043a \u0431\u044b\u0442\u044c \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430?<\/p>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u0438\u0434\u043e\u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043d\u0430\u0448 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IReader.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">IReader&lt;T&gt;<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">public interface IReader&lt;T&gt; {     T Read(T[] arr, int index);     bool Supports&lt;TType&gt;(); }   public class SignedIntegersReader : IReader&lt;Int32&gt;, IReader&lt;Int16&gt;, IReader&lt;Int64&gt; {     int IReader&lt;int&gt;.Read(int[] arr, int index)     {         return arr[index];     }      short IReader&lt;short&gt;.Read(short[] arr, int index)     {         return arr[index];     }      long IReader&lt;long&gt;.Read(long[] arr, int index)     {         return arr[index];     }      public bool Supports&lt;TType&gt;()     {         return this as IReader&lt;TType&gt; != null;     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e IReader \u2014 DefaultReader.<\/p>\n<pre><code class=\"cs\">public class DefaultReader&lt;T&gt; : IReader&lt;T&gt; {     private IReader&lt;T&gt; _reader = new SignedIntegersReader() as IReader&lt;T&gt;;      public T Read(T[] arr, int index)     {         if (_reader != null)         {             return _reader.Read(arr, index);         }         return default(T);     }      public bool Supports&lt;TType&gt;()     {         return _reader.Supports&lt;TType&gt;();     } } <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435:<\/p>\n<pre><code class=\"cs\">class Program {     static void Main(string[] args)     {         var reader = new DefaultReader&lt;int&gt;();           var arr = new int[] { 128, 256 };           if (reader.Supports&lt;int&gt;())         {             for (int i = 0; i &lt; arr.Length; i++)             {                 Console.WriteLine(&quot;Reader result: {0}&quot;, reader.Read(arr, i));             }         }     } } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0434\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0442\u0438\u043f\u0430\u043c \u2013 \u043a\u0430\u043a \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0442\u0430\u043a \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.       \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/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\/201996\/\"> http:\/\/habrahabr.ru\/post\/201996\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u2013 \u043e\u043d\u0438 \u0436\u0435 generics, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u043c\u043e\u0449\u043d\u0435\u0439\u0448\u0438\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438.<\/p>\n<p>  CLR \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0438\u0445 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 MSIL, \u0438 \u0432\u0441\u0435\u0433\u043e \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u0441 \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0412\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 C++, \u043d\u043e \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0442\u043e \u043f\u043e \u0438\u0437\u044f\u0449\u0435\u0441\u0442\u0432\u0443 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u0443\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430 C#, \u0442\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432 \u044d\u0442\u043e\u043c.  <\/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-201996","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/201996","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=201996"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/201996\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=201996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=201996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=201996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}