{"id":1383,"date":"2014-02-13T19:56:51","date_gmt":"2014-02-13T19:56:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1383"},"modified":"2014-02-13T19:56:51","modified_gmt":"2014-02-13T19:56:51","slug":"yield-return-inside-a-using-block","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/yield-return-inside-a-using-block\/","title":{"rendered":"yield return inside a using block"},"content":{"rendered":"<p>This a short post to act as a reminder. <\/p>\n<p>I decided to refactor some code to return IEnumerable<T> (using <em>yield return<\/em>) instead of returning an IList<T>. So I have a method named Deserialize which creates a memory stream and then passes it to an overload which takes a Stream. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic IEnumerable&lt;T&gt; Deserialize(string data)\r\n{\r\n   using (MemoryStream memoryStream = new MemoryStream())\r\n   {\r\n      memoryStream.Write(Encoding.ASCII.GetBytes(data), 0, data.Length);\r\n      memoryStream.Seek(0, SeekOrigin.Begin);\r\n\r\n      return Deserialize(memoryStream);\r\n   }\r\n}\r\n\r\npublic IEnumerable&lt;T&gt; Deserialize(Stream stream)\r\n{\r\n   \/\/ does some stuff\r\n   yield return item;\r\n}\r\n<\/pre>\n<p>The unit tests thankfully spotted straight away a simple mistake. In that the <em>using<\/em> clause is exited before the first <em>yield return<\/em> takes place. So the stream is disposed of, and therefore invalid, when the overload tried to access it. Doh !<\/p>\n<p>Thankfully it&#8217;s easy to fix using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic IEnumerable&lt;T&gt; Deserialize(string data)\r\n{\r\n   using (MemoryStream memoryStream = new MemoryStream())\r\n   {\r\n      memoryStream.Write(Encoding.ASCII.GetBytes(data), 0, data.Length);\r\n      memoryStream.Seek(0, SeekOrigin.Begin);\r\n\r\n      foreach (var item in Deserialize(memoryStream))\r\n      {\r\n         yield return item;\r\n      }   \r\n   }\r\n}\r\n<\/pre>\n<p>Obviously we could have alternatively used something like <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nreturn new List&lt;T&gt;(Deserialize(memoryStream));\r\n<\/pre>\n<p>but this then negates the purpose of using the <em>yield return<\/em> in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This a short post to act as a reminder. I decided to refactor some code to return IEnumerable (using yield return) instead of returning an IList. So I have a method named Deserialize which creates a memory stream and then passes it to an overload which takes a Stream. For example public IEnumerable&lt;T&gt; Deserialize(string data) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1383","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1383"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1383\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1383\/revisions\/1386"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}