{"id":78387,"date":"2022-12-22T08:00:06","date_gmt":"2022-12-22T07:00:06","guid":{"rendered":"https:\/\/code-maze.com\/?p=78387"},"modified":"2022-12-22T09:07:16","modified_gmt":"2022-12-22T08:07:16","slug":"csharp-ienumerable","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-ienumerable\/","title":{"rendered":"IEnumerable in C#"},"content":{"rendered":"<p>In this article, we are going to learn about IEnumerable in C#. IEnumerable acts as an abstraction over a collection and allows us to iterate over it without knowing the actual type of the collection.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/collections-csharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s begin.<\/p>\n<h2><a id=\"ienumerable\"><\/a>What is the IEnumerable Interface in C#?<\/h2>\n<p><strong>The<\/strong> <code>IEnumerable<\/code><strong> interface is the base for all the non-generic collections that can be enumerated. <\/strong><\/p>\n<p>It exposes a single method <code>GetEnumerator()<\/code>. This method returns a reference to yet another interface <code>System.Collections.IEnumerator<\/code>.<\/p>\n<p>The <code>IEnumerator<\/code> interface in turn provides the ability to iterate through a collection by using the methods <code>MoveNext()<\/code> and <code>Reset()<\/code>, and the property <code>Current<\/code>.<\/p>\n<div style=\"padding: 20px; border-left: 5px gray solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">If you&#8217;d like to learn about the different collection types available in .NET, check out our <a href=\"https:\/\/code-maze.com\/dotnet-collections\/\" target=\"_blank\" rel=\"noopener\">.NET Collections series<\/a>.<\/div>\n<p><strong>Hence, in order for us to be able to use <code>ForEach<\/code> over a collection, it must implement <code>IEnumerable<\/code>.<\/strong><\/p>\n<h2><a id=\"ext\"><\/a>Extension Methods in IEnumerable<\/h2>\n<p>The <code>IEnumerable<\/code> interface includes some <a href=\"https:\/\/code-maze.com\/csharp-static-members-constants-extension-methods\/#extensionmethods\" target=\"_blank\" rel=\"noopener\">extension methods<\/a> along with the <code>GetEnumerator()<\/code> method.\u00a0<\/p>\n<p><strong>We use deferred execution to implement these methods, so the actual value of the return object is realized only when the object is enumerated<\/strong> either using a <code>ForEach<\/code> loop or by calling its <code>GetEnumerator()<\/code> method.<\/p>\n<h3><a id=\"cast\"><\/a>Cast&lt;TResult&gt;<\/h3>\n<p>This method takes a sequence of type <code>IEnumerable<\/code> and converts all the objects of the given sequence to the type <code>TResult<\/code>, thus resulting in a return type <code>IEnumerable&lt;TResult&gt;<\/code>.<\/p>\n<p>The <code>Cast&lt;TResult&gt;()<\/code> method is a <a href=\"https:\/\/code-maze.com\/using-generics-in-csharp\/\" target=\"_blank\" rel=\"noopener\">generic method<\/a> where <code>TResult<\/code> acts as a placeholder for the actual type we want to work with at compile time. It allows the use of standard <a href=\"https:\/\/code-maze.com\/linq-sorting-and-filtering\/\" target=\"_blank\" rel=\"noopener\">LINQ query operators<\/a> like filtering, sorting, etc. on a non-generic type such as <code>Arraylist<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"9\">var primes = new ArrayList\r\n{\r\n    5,\r\n    3,\r\n    2,\r\n    7\r\n};\r\n\r\nvar primeQuery = primes.Cast&lt;int&gt;().OrderBy(prime =&gt; prime).Select(prime =&gt; prime);\r\n\r\nvar expected = new List&lt;int&gt; { 2, 3, 5, 7 };\r\n\r\nAssert.Equal(expected, primeQuery);<\/pre>\n<p>We are able to convert a non-generic collection of type <code>Arraylist<\/code> into <code>IEnumerable&lt;int&gt;<\/code>and thus have all its functionalities available.<\/p>\n<h3><a id=\"oftype\"><\/a>OfType&lt;TResult&gt;<\/h3>\n<p>This method takes a sequence of type <code>IEnumerable<\/code> and filters it based on the specified type. <strong>It returns only those elements from the input source that can be cast to the type <code>TResult<\/code><\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"11\">var cities = new ArrayList\r\n{\r\n    \"London\",\r\n    \"Paris\",\r\n    \"Madrid\",\r\n    \"Berlin\",\r\n    7,\r\n    \"Lisbon\"\r\n};\r\n\r\nvar cityQuery = cities.OfType&lt;string&gt;();\r\n\r\nvar expected = new List&lt;string&gt; { \"London\", \"Paris\", \"Madrid\", \"Berlin\", \"Lisbon\" };\r\n\r\nAssert.Equal(expected, cityQuery);<\/pre>\n<p>This is where it differs from <code>Cast&lt;TResult&gt;()<\/code> which instead throws an exception if an element can&#8217;t be cast to <code>TResult<\/code>. We can also use standard query operators such as <code>Where()<\/code> after filtering the source:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"13\">var cities = new ArrayList\r\n{\r\n    \"London\",\r\n    \"Paris\",\r\n    \"Madrid\",\r\n    \"Berlin\",\r\n    7,\r\n    \"Lisbon\",\r\n    8,\r\n    12\r\n};\r\n\r\nvar evens = cities.OfType&lt;int&gt;().Where(x =&gt; x % 2 == 0);\r\n\r\nvar expected = new List&lt;int&gt; { 8, 12 };\r\n\r\nAssert.Equal(expected, evens);<\/pre>\n<h3><a id=\"aspara\"><\/a>AsParallel Method<\/h3>\n<p>This method enables the parallelization of a query. It takes a sequence of type <code>IEnumerable<\/code> and converts it to the type <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.linq.parallelquery?view=net-6.0\" target=\"_blank\" rel=\"nofollow noopener\">ParallelQuery<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3\">var numbers = Enumerable.Range(0, 100);\r\n\r\nvar oddsCount = numbers.AsParallel().Count(x =&gt; x % 2 != 0);\r\n\r\nvar expected = 50;\r\n\r\nAssert.Equal(expected, oddsCount);<\/pre>\n<p>The <code>AsParallel()<\/code>method binds the query to <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/parallel-programming\/introduction-to-plinq\" target=\"_blank\" rel=\"nofollow noopener\">PLINQ<\/a> or <strong>Parallel LINQ<\/strong>. The PLINQ queries work in a similar way to the sequential LINQ queries where they operate on in-memory data sources and allow deferred execution.<\/p>\n<p>However, they attempt to make use of all available processors by partitioning the data source into segments and then executing the query on each segment on separate worker threads in parallel on multiple processors.\u00a0<\/p>\n<h3><a id=\"asquery\"><\/a>AsQueryable Method<\/h3>\n<p>This method takes a sequence of type <code>IEnumerable<\/code> and converts it into <a href=\"https:\/\/code-maze.com\/dotnet-collections-ienumerable-iqueryable-icollection\/#iqueryable\" target=\"_blank\" rel=\"noopener\">IQueryable<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"6\">var numbers = new List&lt;int&gt;\r\n{\r\n    1, 2, 3, 4, 5, 6, 7, 8, 9, 10\r\n};\r\n\r\nvar query = numbers.AsQueryable();\r\n\r\nvar multiplesOf3 = query.Where(x =&gt; x % 3 == 0);\r\n\r\nvar expected = new List&lt;int&gt; { 3, 6, 9 };\r\n\r\nAssert.Equal(expected, multiplesOf3);\r\n\r\nAssert.Equal(\"Constant\", query.Expression.NodeType.ToString());<\/pre>\n<p>The <code>IQueryable<\/code> interface extends the <code>IEnumerable<\/code> interface and hence is similar in functionality.<\/p>\n<p>The main difference between both is that while <code>IEnumerable<\/code> is more suited to collections loaded using LINQ where filtering is required on the client side where <code>IEnumerable<\/code> is, <code>IQueryable<\/code> creates a SQL Query on the server side and only filtered data is sent to the client side.<\/p>\n<p>This concludes the extension methods of IEnumerable in C#. Let&#8217;s now take a look at implementing a custom IEnumerable interface in code.<\/p>\n<h2><a id=\"code\"><\/a>Code Implementation of IEnumerable Interface<\/h2>\n<p>Let&#8217;s start by creating a <code>Book<\/code>class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Book\r\n{\r\n    public Book(string name, string author)\r\n    {\r\n        Name = name;\r\n        Author = author;\r\n    }\r\n\r\n    public string Name { get; set; }\r\n    public string Author { get; set; }\r\n}<\/pre>\n<p>Now, that we have our business object, let&#8217;s create a collection of this object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Library : IEnumerable\r\n{\r\n    private readonly Book[] _books;\r\n\r\n    public Library(Book[] books)\r\n    {\r\n        _books = new Book[books.Length];\r\n\r\n        for (int i = 0; i &lt; books.Length; i++)\r\n        {\r\n            _books[i] = books[i];\r\n        }\r\n    }\r\n}<\/pre>\n<p>The class <code>Library<\/code> implements <code>IEnumerable<\/code> so that we may use the <code>ForEach<\/code> loop on the class objects. However, the code does not compile until we implement the <code>GetEnumerator()<\/code> method of the interface.<\/p>\n<p>As we need to implement <code>IEnumerator<\/code> when implementing <code>IEnumerable<\/code>, let&#8217;s first implement the <code>IEnumerator<\/code> interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class BookEnumerator : IEnumerator\r\n{\r\n    private readonly Book[] _books;\r\n    private int _index;\r\n\r\n    public BookEnumerator(Book[] books)\r\n    {\r\n        _books = books;\r\n    }\r\n\r\n    public Book Current\r\n    {\r\n        get\r\n        {\r\n            return _books[_index];\r\n        }\r\n    }\r\n\r\n    object IEnumerator.Current =&gt; Current;\r\n\r\n    public bool MoveNext()\r\n    {\r\n        _index++;\r\n        return _index &lt; _books.Length;\r\n    }\r\n\r\n    public void Reset()\r\n    {\r\n        _index = -1;\r\n    }\r\n}<\/pre>\n<p>We start by implementing the required members of <code>IEnumerator<\/code> in the class <code>BookEnumerator<\/code> i.e. the property <code>Current<\/code>, and the methods <code>Reset()<\/code> and <code>MoveNext()<\/code>.<\/p>\n<p>When the class is instantiated, the enumerator <code>_index<\/code> is placed before the first element of the collection. When iterating over the collection, we send a call to <code>MoveNext()<\/code> that advances the enumerator to the first position.\u00a0<\/p>\n<p>The <code>Current<\/code> property indicates the position of the enumerator at any point. It returns the same value until <code>MoveNext()<\/code> or <code>Reset()<\/code> are called. It is undefined under these conditions:<\/p>\n<ul>\n<li>When the enumerator places before the first element of the collection, immediately after the instantiation<\/li>\n<li>When the last call to <code>MoveNext()<\/code> returns false i.e. the iteration of the collection is complete<\/li>\n<li>If any change in the collection such as addition, modification, etc. invalidates the enumerator<\/li>\n<\/ul>\n<p>The <code>Reset()<\/code> method resets the enumerator position to the original. The <code>MoveNext()<\/code> method advances <code>_index<\/code> till it reaches the position after the last element of the collection. After this, the <code>MoveNext()<\/code> method always returns <code>false<\/code>.<\/p>\n<p>With this step done, we are now ready to revisit our <code>Library<\/code> class and get rid of the compiler errors due to not implementing the <code>GetEnumerator()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Library : IEnumerable\r\n{\r\n    \/\/Existing Code excluded for brevity\r\n\r\n    public BookEnumerator GetEnumerator() =&gt; new BookEnumerator(_books);\r\n        \r\n    IEnumerator IEnumerable.GetEnumerator() =&gt; GetEnumerator();\r\n        \r\n}<\/pre>\n<p>Now let&#8217;s see the <code>Library<\/code> class in action as an implementation of <code>IEnumerable<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var books = new Book[5]\r\n{\r\n    new Book(\"Anna Karenina\", \"Leo Tolstoy\"),\r\n    new Book(\"To Kill a Mockingbird\", \"Harper Lee\"),\r\n    new Book(\"The Great Gatsby\", \"F. Scott Fitzgerald\"),\r\n    new Book(\"One Hundred Years of Solitude\", \"Gabriel Garc\u00eda M\u00e1rquez\"),\r\n    new Book(\"A Passage to India\", \"E.M. Forster\")\r\n};\r\n\r\nvar library = new Library(books);\r\n\r\nforeach (var book in library)\r\n{\r\n    Console.WriteLine($\"Book: {book.Name}, Author: {book.Author}\");\r\n}<\/pre>\n<p>The IEnumerable interface allows the use of <code>ForEach<\/code> loop on the collection of type <code>Library<\/code>. On running the program we receive the expected output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Book: Anna Karenina, Author: Leo Tolstoy\r\nBook: To Kill a Mockingbird, Author: Harper Lee\r\nBook: The Great Gatsby, Author: F. Scott Fitzgerald\r\nBook: One Hundred Years of Solitude, Author: Gabriel Garc\u00eda M\u00e1rquez\r\nBook: A Passage to India, Author: E.M. Forster<\/pre>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>In this article, we learned about IEnumerable in C#. We learned how it is necessary to implement for us to iterate over a collection. We learned the associated extension methods and their implementations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about IEnumerable in C#. IEnumerable acts as an abstraction over a collection and allows us to iterate over it without knowing the actual type of the collection. Let&#8217;s begin. What is the IEnumerable Interface in C#? The IEnumerable interface is the base for all the non-generic collections [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[948],"class_list":["post-78387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-ienumerable","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>IEnumerable in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-ienumerable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"IEnumerable in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-ienumerable\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-22T07:00:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-22T08:07:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"IEnumerable in C#\",\"datePublished\":\"2022-12-22T07:00:06+00:00\",\"dateModified\":\"2022-12-22T08:07:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/\"},\"wordCount\":852,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"IEnumerable\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-ienumerable\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/\",\"url\":\"https:\/\/code-maze.com\/csharp-ienumerable\/\",\"name\":\"IEnumerable in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-12-22T07:00:06+00:00\",\"dateModified\":\"2022-12-22T08:07:16+00:00\",\"description\":\"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-ienumerable\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-ienumerable\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IEnumerable in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"IEnumerable in C# - Code Maze","description":"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-ienumerable\/","og_locale":"en_US","og_type":"article","og_title":"IEnumerable in C# - Code Maze","og_description":"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection","og_url":"https:\/\/code-maze.com\/csharp-ienumerable\/","og_site_name":"Code Maze","article_published_time":"2022-12-22T07:00:06+00:00","article_modified_time":"2022-12-22T08:07:16+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"IEnumerable in C#","datePublished":"2022-12-22T07:00:06+00:00","dateModified":"2022-12-22T08:07:16+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/"},"wordCount":852,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["IEnumerable"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-ienumerable\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-ienumerable\/","url":"https:\/\/code-maze.com\/csharp-ienumerable\/","name":"IEnumerable in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-12-22T07:00:06+00:00","dateModified":"2022-12-22T08:07:16+00:00","description":"The IEnumerable interface is the base for all the non-generic collections that can be enumerated. It helps us iterate through a collection","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-ienumerable\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-ienumerable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"IEnumerable in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/78387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=78387"}],"version-history":[{"count":3,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/78387\/revisions"}],"predecessor-version":[{"id":79091,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/78387\/revisions\/79091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=78387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=78387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=78387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}