{"id":176,"date":"2009-07-24T22:32:07","date_gmt":"2009-07-24T21:32:07","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=176"},"modified":"2009-07-24T22:32:45","modified_gmt":"2009-07-24T21:32:45","slug":"yielding-results","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/yielding-results\/","title":{"rendered":"Yielding results"},"content":{"rendered":"<p>Yield is a new syntax word within the .net 2 language.  This allows for the IEnumerable interface to have direct interaction instead of using the IEnumerator interface to iterate though a result set.  For easier demonstration, I think, the code examples, the first code is a .net 1 version of the interface IEnumerable.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Collections;\r\n\r\n\/\/ the inital class is IEnumerable- means that it has to implement the GetEnumerator\r\npublic class Class : IEnumerable \r\n{\r\n       \/\/ internal class to implement the IEnumerator interface, which inturn has to do the MoveNext, Current, Reset methods.\r\n       private class ClassEnumerator : IEnumerator\r\n       {\r\n              \/\/ internal values to go through the array\r\n              private int index = -1;\r\n              private Class P;\r\n              \/\/ setup the internal variable\r\n              public ClassEnumerator(Class P) \r\n              {\r\n                     this.P = P; \r\n              }\r\n              \/\/ move within the array, if any values are present.\r\n              public bool MoveNext()\r\n              {\r\n                     index++;\r\n                     return index < P._Names.Length;\r\n              }\r\n              \/\/ set the internal to -1, thus restart\r\n              public void Reset() \r\n              { \r\n                     index = -1;\r\n              }\r\n\r\n              \/\/ get the Current object\r\n              public object Current \r\n              { \r\n                     get \r\n                     { \r\n                            return P._Names[index]; \r\n                     } \r\n              }\r\n       }\r\n       \r\n       \/\/ return a IEnumerator object of the above with itself passed\r\n       public IEnumerator GetEnumerator()\r\n       {\r\n              return new ClassEnumerator(this);\r\n       }\r\n\r\n       \/\/ the internal string array of names\r\n       private string[] _Names;\r\n       \/\/ the new class setup process, \r\n       public Class(params string[] Names)\r\n       {\r\n              _Names = new string[Names.Length];\r\n              \/\/ copy the passed parameter starting at position 0\r\n              Names.CopyTo(_Names,0);\r\n       }\r\n}\r\n\r\nclass ClassProgram\r\n{\r\n       static void Main(string[] args)\r\n       {\r\n              Class ClassArr = new Class(\"Student1\", \"Student2\",\"Studentx\");\r\n              foreach (string s in ClassArr)\r\n                     Console.WriteLine(s);\r\n       }\r\n}\r\n<\/pre>\n<p>This is able to output a string of <\/p>\n<pre class=\"consoleoutput\">\r\nStudent1\r\nStudent2\r\nStudentx\r\n<\/pre>\n<p>Since the Class class (it is a class of students), there is allot of code to iterate with the foreach function.<\/p>\n<p>Within .net 2 there is a nice and more tighter method called yield, an yield will yield a result and be able to iterate though a result set with the IEnumerable return set.  Again, here is the source code.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Collections;\r\n\r\nclass yielding\r\n{\r\n       \/\/ same as the class as in .net 1 version.\r\n       public class Class : IEnumerable\r\n       {\r\n              \/\/ but only need to implement this one method to do the same as the pervious separate class\r\n              public IEnumerator GetEnumerator()\r\n              {\r\n                     int counter = -1;\r\n                     while (counter++ < (_Names.Length-1))\r\n                            yield return _Names[counter];\r\n              }\r\n\r\n              \/\/ same as the other .net 1 class internals for the private variable and the constructor\r\n              private string[] _Names;              \r\n              public Class(params string[] Names)\r\n              {\r\n                     _Names = new string[Names.Length];\r\n                     Names.CopyTo(_Names,0);\r\n              }\r\n       }       \r\n       \r\n       static void Main(string[] args)\r\n       {\r\n              \/\/ same as the .net 1 version\r\n              Class ClassArr = new Class(\"Student1\", \"Student2\", \"Studentx\");\r\n              foreach (string s in ClassArr)\r\n                     Console.WriteLine(s);\r\n       }\r\n}\r\n<\/pre>\n<p>Far less code and also the result is exactly the same.<\/p>\n<p>Also there are other benefits of the yield attached to an IEnumerable interface, here is some more code to demonstrate how to implement a power function to display the power result being built up.  This demonstrates how to use just a single method to return an iterate-able result.<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Collections;\r\n\r\nclass yielding\r\n{\r\n       \/\/ same as the class as in .net 1 version.\r\n       public class Class : IEnumerable\r\n       {\r\n              \/\/ but only need to implement this one method to do the same as the pervious separate class\r\n              public IEnumerator GetEnumerator()\r\n              {\r\n                     int counter = -1;\r\n                     while (counter++ < (_Names.Length-1))\r\n                            yield return _Names[counter];\r\n              }\r\n\r\n              \/\/ same as the other .net 1 class internals for the private variable and the constructor\r\n              private string[] _Names;              \r\n              public Class(params string[] Names)\r\n              {\r\n                     _Names = new string[Names.Length];\r\n                     Names.CopyTo(_Names,0);\r\n              }\r\n       }       \r\n       \r\n       \/\/ IEnumerable linked to yield is new in .net 2, it allows for the \r\n       \/\/ iteration of this method, acting like array instead of a single return.\r\n       public static IEnumerable Power(int number, int exp)\r\n       {\r\n              int counter = 0, result = 1;       \/\/ setup the default values.\r\n              while (counter++ < exp)              \/\/ increament counter and whilst lower than the passed parameter exp\r\n              {\r\n                     result *= number;       \/\/ result = result * number;\r\n                     yield return result;       \/\/ yield the result and return\r\n              }\r\n       }\r\n\r\n       \/\/ can also just iterate though a array of strings (in this example)\r\n       public static IEnumerable StReturn(params string[] names)\r\n       {\r\n              int counter = -1;\r\n              while (counter++ < (names.Length-1))\r\n                     yield return names[counter];\r\n       }\r\n\r\n       static void Main(string[] args)\r\n       {\r\n              \/\/ can do a return of a power function as well\r\n              foreach(int e in Power(4,3))\r\n                     Console.WriteLine(e.ToString());\r\n              \r\n              \/\/ you can just iterate a string \r\n              string[] st = {\"student1\",\"student2\",\"studentx\"};\r\n              foreach(string s in StReturn(st))\r\n                     Console.WriteLine(s.ToString());\r\n              \r\n              \/\/ same as the .net 1 version\r\n              Class ClassArr = new Class(\"Student1\", \"Student2\", \"Studentx\");\r\n              foreach (string s in ClassArr)\r\n                     Console.WriteLine(s);\r\n       }\r\n}\r\n<\/pre>\n<p>I have also include the code from above to demonstrate the similar usage for iterate though a string array.<\/p>\n<p>Output would be<\/p>\n<pre class=\"consoleoutput\">\r\n4\r\n16\r\n64\r\nstudent1\r\nstudent2\r\nstudentx\r\nStudent1\r\nStudent2\r\nStudentx\r\n<\/pre>\n<p>You will need to have .net 2 installed or <a title=\"mono\"  href=\"http:\/\/www.mono-project.com\/Main_Page\" target=\"_blank\">mono<\/a> (use the gmcs part of mono to use the .net 2 functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yield is a new syntax word within the .net 2 language. This allows for the IEnumerable interface to have direct interaction instead of using the IEnumerator interface to iterate though a result set. For easier demonstration, I think, the code examples, the first code is a .net 1 version of the interface IEnumerable. using System; &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/yielding-results\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Yielding results<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[14,398,13,400],"class_list":["post-176","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-c-sharp","tag-linux","tag-mono","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=176"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/176\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}