{"id":282,"date":"2023-07-12T05:10:50","date_gmt":"2023-07-12T04:10:50","guid":{"rendered":"https:\/\/ishooscode.com\/?p=282"},"modified":"2023-07-12T05:13:24","modified_gmt":"2023-07-12T04:13:24","slug":"enumerator-or-foreach","status":"publish","type":"post","link":"https:\/\/ishooscode.com\/index.php\/2023\/07\/12\/enumerator-or-foreach\/","title":{"rendered":"What is better between Enumerator or foreach"},"content":{"rendered":"\n<p>In this article we will try to bring some light on Enumerator or foreach. What is Better? Both has same motive, to perform loop. One allow us to have more control over looping and other provide simple syntax. This also brings light on very familiar concept of foreach loop. Exactly how foreach loop works and what is internal processing. Lets first dig into enumerator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enumerator or foreach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Enumerator <\/h3>\n\n\n\n<p>IEnumerator is used to go through a list with significant control. It has one object type property, i.e., Current which has getter only and two methods,<\/p>\n\n\n\n<ul>\n<li>MoveNext that returns boolean<\/li>\n\n\n\n<li>Reset<\/li>\n<\/ul>\n\n\n\n<p><strong>Code sample<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IEnumerator&nbsp;enumerator&nbsp;=&nbsp;enumerable.GetEnumerator();&nbsp;&nbsp;\n\nwhile&nbsp;(enumerator.MoveNext())&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;element&nbsp;=&nbsp;enumerator.Current;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Perform&nbsp;your&nbsp;logic&nbsp;logic&nbsp;on&nbsp;the&nbsp;element&nbsp;&nbsp;\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s consider a scenario where you have to loop through a collection in two different method take the example above,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;int&gt;&nbsp;enumerable&nbsp;=&nbsp;new&nbsp;List&lt;int&gt;();&nbsp;&nbsp;\n\nfor&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;=&nbsp;20;&nbsp;i++)&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;enumerable.Add(i);&nbsp;&nbsp;\n}&nbsp;&nbsp;\n\nIEnumerator&nbsp;enumerator&nbsp;=&nbsp;enumerable.GetEnumerator();&nbsp;&nbsp;\nConsole.WriteLine(\"FirstLoopMethod&nbsp;start...\");&nbsp;&nbsp;\n\nwhile&nbsp;(enumerator.MoveNext())&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;element&nbsp;=&nbsp;enumerator.Current;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;num&nbsp;=&nbsp;int(element);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(num);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if(num&nbsp;&gt;&nbsp;10)&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecondLoopMethod(enumerator);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;you&nbsp;logic&nbsp;here&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Perform&nbsp;your&nbsp;logic&nbsp;on&nbsp;the&nbsp;element&nbsp;&nbsp;\n}&nbsp;&nbsp;\n\nprivate&nbsp;void&nbsp;SecondLoopMethod(IEnumerator&nbsp;enumerator)&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine();&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"SecondLoopMethod&nbsp;start...\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(enumerator.MoveNext())&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;element&nbsp;=&nbsp;enumerator.Current;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;num&nbsp;=&nbsp;int(element);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(num);&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Perform&nbsp;your&nbsp;logic&nbsp;logic&nbsp;on&nbsp;the&nbsp;element&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n}<\/code><\/pre>\n\n\n\n<p>This is the output you get,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/1.bp.blogspot.com\/-SQeOvkMD3eo\/XWFCNQYjfpI\/AAAAAAAAYr0\/ph1DSwNsywAPlTXfdlvNgKfLWm8PCHr0ACLcBGAs\/s1600\/Capture.PNG\"><img decoding=\"async\" src=\"https:\/\/f4n3x6c5.stackpathcdn.com\/UploadFile\/BlogImages\/09272019042131AM\/Result.png\" alt=\"Enumerator or foreach\"\/><\/a><\/figure>\n\n\n\n<p>Here you can see in the SecondLoopMethod the iteration started from where we left it in the first method.<\/p>\n\n\n\n<p>That&#8217;s the benefit and control of using IEnumerator.<\/p>\n\n\n\n<p>Where if we try to this with IEnumerable then in the second method it again starts looping from the beginning<\/p>\n\n\n\n<p>Here is the documentation on&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.ienumerator?redirectedfrom=MSDN&amp;view=netframework-4.8\">Enumerator<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enumerable \/ foreach<\/h3>\n\n\n\n<p>We can loop through items of the list using IEnumerator too. IEnumerable and IEnumerator are both interfaces in the .net framework.<\/p>\n\n\n\n<p>IEnumerable has just one method declaration i.e. GetEnumerator which return an enumerator.<\/p>\n\n\n\n<p>Every object which can be looped through using foreach, inherits from IEnumerable directly or indirectly.<\/p>\n\n\n\n<p>You cannot use foreach on classes which do not inherit from IEnumerable<\/p>\n\n\n\n<p>If you have a model and want to loop through its array using foreach then that class must inherit from IEnumerable<\/p>\n\n\n\n<p><strong>Code sample<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach(object&nbsp;item&nbsp;in&nbsp;enumerable)&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Your&nbsp;logic&nbsp;on&nbsp;the&nbsp;item&nbsp;&nbsp;\n}<\/code><\/pre>\n\n\n\n<p>When your code executes the above code becomes.<\/p>\n\n\n\n<p>While debugging the code I found following things<\/p>\n\n\n\n<ul>\n<li>When debugger was at `enumerable` in above foreach loop the debugger move to GetEnumerator() method in class&nbsp;<\/li>\n\n\n\n<li>When debugger was at in keyword in loop MoveNext method of Enumerator class was called<\/li>\n\n\n\n<li>When debugger was at item it loaded the object in it&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>IEnumerator&nbsp;enumerator&nbsp;=&nbsp;enumerable.GetEnumerator();&nbsp;&nbsp;\n\nwhile&nbsp;(enumerator.MoveNext())&nbsp;&nbsp;\n{&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;object&nbsp;item&nbsp;=&nbsp;enumerator.Current;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Perform&nbsp;logic&nbsp;on&nbsp;the&nbsp;item&nbsp;&nbsp;\n}<\/code><\/pre>\n\n\n\n<p>Documentation for&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.collections.ienumerable?view=netframework-4.8\" target=\"_blank\" rel=\"noreferrer noopener\">Enumerable<\/a>.<\/p>\n\n\n\n<p>Now what are the benefits and drawbacks of both?<\/p>\n\n\n\n<p>IEnumerable simplifies and shortens the original looping syntax compared to IEnumerator<br>IEnumerator provides better flexibility and control over your looping logic<\/p>\n\n\n\n<p>Cheers!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">More posts like this<\/h4>\n\n\n<ul class=\"wp-block-latest-posts__list wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/ishooscode.com\/index.php\/2023\/12\/13\/introduction-to-azure-cosmos-db\/\">Introduction to Azure Cosmos DB<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/ishooscode.com\/index.php\/2023\/09\/02\/enhancing-app-security-with-azure-managed-identities\/\">Enhancing app security with Azure Managed Identities<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/ishooscode.com\/index.php\/2023\/08\/23\/comparing-performance-of-lists\/\">IL code performance of Generic List And Non Generic List<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/30\/how-does-a-dictionary-maintains-data\/\">How does a dictionary maintains data<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/12\/enumerator-or-foreach\/\">What is better between Enumerator or foreach<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>In this article we will try to bring some light on Enumerator or foreach. What&#8230;<\/p>\n","protected":false},"author":1,"featured_media":284,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[22],"tags":[20,15],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/282"}],"collection":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/comments?post=282"}],"version-history":[{"count":2,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/282\/revisions\/285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media\/284"}],"wp:attachment":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}