{"id":200,"date":"2025-10-07T08:52:39","date_gmt":"2025-10-07T06:52:39","guid":{"rendered":"https:\/\/www.insync.co.za\/blog\/?p=200"},"modified":"2025-10-07T09:09:22","modified_gmt":"2025-10-07T07:09:22","slug":"understanding-ienumerable-vs-icollection-in-c","status":"publish","type":"post","link":"https:\/\/www.insync.co.za\/blog\/2025\/10\/07\/understanding-ienumerable-vs-icollection-in-c\/","title":{"rendered":"Understanding IEnumerable<T> vs ICollection<T> in C#"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When working with collections in C#, two interfaces are foundational: <code>IEnumerable&lt;T&gt;<\/code> and <code>ICollection&lt;T&gt;<\/code>. Knowing the difference between them is crucial for writing efficient, maintainable, and correct code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is <code>IEnumerable&lt;T&gt;<\/code>?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>IEnumerable&lt;T&gt;<\/code> is the most basic interface for collections in .NET. It allows <strong>forward-only iteration<\/strong> over a sequence of items.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Defined in <code>System.Collections.Generic<\/code>.<\/li>\n\n\n\n<li>Provides the <code>GetEnumerator()<\/code> method to iterate items.<\/li>\n\n\n\n<li>Supports <strong>deferred execution<\/strong> in LINQ queries, meaning the sequence is only evaluated when enumerated.<\/li>\n\n\n\n<li>Ideal for <strong>read-only access<\/strong> or streaming large datasets.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-1f014673ce93aaee673eae4b842f79f4\"><code>IEnumerable&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5 };\nforeach (var number in numbers)\n{\n    Console.WriteLine(number);\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Notice: <code>IEnumerable<\/code> doesn\u2019t provide a way to add, remove, or count elements efficiently.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reference:<\/strong> <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.icollection-1?view=net-9.0\">Microsoft Docs \u2013 IEnumerable&lt;T><\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is <code>ICollection&lt;T&gt;<\/code>?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ICollection&lt;T&gt;<\/code> extends <code>IEnumerable&lt;T&gt;<\/code> and adds features for <strong>collection management<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Also defined in <code>System.Collections.Generic<\/code>.<\/li>\n\n\n\n<li>Includes properties like <code>Count<\/code> and <code>IsReadOnly<\/code>.<\/li>\n\n\n\n<li>Provides methods such as <code>Add()<\/code>, <code>Remove()<\/code>, <code>Contains()<\/code>, and <code>CopyTo()<\/code>.<\/li>\n\n\n\n<li>Suitable when you need to <strong>know the number of elements<\/strong> or <strong>modify the collection<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-2df89e839ca35dc945101715fb2f06f1\"><code>ICollection&lt;string&gt; names = new List&lt;string&gt; { \"Alice\", \"Bob\", \"Charlie\" };\nnames.Add(\"David\");\nConsole.WriteLine($\"Total names: {names.Count}\");\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reference:<\/strong> <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.icollection-1?view=net-9.0\">Microsoft Docs \u2013 ICollection&lt;T><\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Lorem Ipsum has been the industry&#8217;s standard dummy text ever since the 1500s.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Differences<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th><code>IEnumerable&lt;T&gt;<\/code><\/th><th><code>ICollection&lt;T&gt;<\/code><\/th><\/tr><\/thead><tbody><tr><td>Purpose<\/td><td>Iterate items<\/td><td>Iterate + modify + count<\/td><\/tr><tr><td>Methods<\/td><td><code>GetEnumerator()<\/code><\/td><td><code>Add()<\/code>, <code>Remove()<\/code>, <code>Contains()<\/code>, <code>CopyTo()<\/code><\/td><\/tr><tr><td>Properties<\/td><td>None<\/td><td><code>Count<\/code>, <code>IsReadOnly<\/code><\/td><\/tr><tr><td>Performance<\/td><td>Lightweight, deferred execution<\/td><td>Slightly heavier, supports modification<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">When to Use Each<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use <code>IEnumerable&lt;T><\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>When you only need to <strong>read or iterate<\/strong> items.<\/li>\n\n\n\n<li>When working with <strong>LINQ queries<\/strong> for deferred execution.<\/li>\n\n\n\n<li>When dealing with <strong>large datasets<\/strong> to reduce memory usage.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Use <code>ICollection&lt;T><\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>When you need to <strong>count items<\/strong> frequently.<\/li>\n\n\n\n<li>When you want to <strong>add, remove, or modify<\/strong> elements.<\/li>\n\n\n\n<li>When a <strong>fixed-size or modifiable collection<\/strong> is required.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Performance Considerations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calling <code>.Count()<\/code> on an <code>IEnumerable&lt;T><\/code> triggers enumeration over all elements, which can be costly. Using <code>ICollection&lt;T><\/code> avoids this because it provides a built-in <code>Count<\/code> property.<\/li>\n\n\n\n<li>Multiple iterations over an <code>IEnumerable&lt;T><\/code> may result in repeated computations if the sequence is generated on-the-fly. Consider converting to a list with <code>.ToList()<\/code> if multiple enumerations are needed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Reference:<\/strong> <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/collections\/\" data-type=\"link\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/collections\/\">Microsoft Docs \u2013 Collections Overview<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>IEnumerable&lt;T&gt;<\/code> and <code>ICollection&lt;T&gt;<\/code> serve different purposes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>IEnumerable&lt;T><\/code><\/strong> is perfect for <strong>iteration and streaming<\/strong>.<\/li>\n\n\n\n<li><strong><code>ICollection&lt;T><\/code><\/strong> is suitable for <strong>counting and modification<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these differences helps you <strong>write more efficient, readable, and maintainable code<\/strong> in C#.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with collections in C#, two interfaces are foundational: IEnumerable&lt;T&gt; and ICollection&lt;T&gt;. Knowing the difference between them is crucial for writing efficient, maintainable, and correct code. What is IEnumerable&lt;T&gt;? IEnumerable&lt;T&gt; is the most basic interface for collections in .NET. It allows forward-only iteration over a sequence of items. Key points: Example: Notice: IEnumerable doesn\u2019t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":207,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[30,28,38],"class_list":["post-200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-c","tag-software-development","tag-webapi"],"_links":{"self":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":2,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions\/206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/media\/207"}],"wp:attachment":[{"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.insync.co.za\/blog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}