<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: ASP.net C# &#8211; List vs IList</title>
	<atom:link href="http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/feed/" rel="self" type="application/rss+xml" />
	<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/</link>
	<description>Pessimistic programmer, optimistic human.</description>
	<lastBuildDate>Fri, 12 Apr 2013 19:43:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Allen Conway</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-529</link>
		<dc:creator>Allen Conway</dc:creator>
		<pubDate>Tue, 19 Oct 2010 15:38:16 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-529</guid>
		<description><![CDATA[You can look at this argument from several angles including the one of a purely OO approach which says to program against an Interface not an implementation.  With this thought, using IList follows the same principal as passing around and using Interfaces that you define from scratch. I also believe in the scalability and flexibility factors provided by an Interface in general. If the implementation of the class returning IList needs to be extended or changed, the consuming code does not have to change.  However if a concrete implementation and List was returned, then changing the classes implementation would cause the calling code to need to be changed as well. This is because a class adhering to IList guarantees a certain behavior that is not guaranteed by a concrete type.  

I see your points though and agree the additional casting back to a List to use the operations needed and checking whether the cast was successful can be tedious.  I wouldn’t label the &#039;size&#039; of the project as the hard line in the sand factor on using Interfaces; we all know the &#039;small&#039; projects that balloon into the enterprise projects and those are the ones built with the worst foundation because it was originally thought of to be a &#039;small&#039; project.  Rather, look at potential scaling of the application, and it &#039;could&#039; blossom into something larger, and then heavily consider a more OO design using Interfaces like IList.]]></description>
		<content:encoded><![CDATA[<p>You can look at this argument from several angles including the one of a purely OO approach which says to program against an Interface not an implementation.  With this thought, using IList follows the same principal as passing around and using Interfaces that you define from scratch. I also believe in the scalability and flexibility factors provided by an Interface in general. If the implementation of the class returning IList needs to be extended or changed, the consuming code does not have to change.  However if a concrete implementation and List was returned, then changing the classes implementation would cause the calling code to need to be changed as well. This is because a class adhering to IList guarantees a certain behavior that is not guaranteed by a concrete type.  </p>
<p>I see your points though and agree the additional casting back to a List to use the operations needed and checking whether the cast was successful can be tedious.  I wouldn’t label the &#8216;size&#8217; of the project as the hard line in the sand factor on using Interfaces; we all know the &#8216;small&#8217; projects that balloon into the enterprise projects and those are the ones built with the worst foundation because it was originally thought of to be a &#8216;small&#8217; project.  Rather, look at potential scaling of the application, and it &#8216;could&#8217; blossom into something larger, and then heavily consider a more OO design using Interfaces like IList.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-299</link>
		<dc:creator>Colin</dc:creator>
		<pubDate>Wed, 14 Jul 2010 21:09:03 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-299</guid>
		<description><![CDATA[While reading the MVC2.0 book, I noticed the AsReadOnly() method, they returned through as an IList&lt;SomethingICantRemember&gt; so I came back to my code, set up the methods to IList and tested it.  Not a single bit of difference.  I then put on the .AsReadOnly() when returning the items, and guess what.  The first code example above was 40 secs faster this time around!  I couldn&#039;t believe what I was seeing so had to do it a few times!!  And that was with:

if(searches is List&lt;SiteMap_Search&gt;)
                output = (searches as List&lt;SiteMap_Search&gt;).FindAll(search =&gt; search.Town.Contains(contains));

so I could use the FindAll() method :D  However what I will have to give Dave credit for is that he made me try other methods and it turns out FindAll() isn&#039;t all that efficient.  By changing the code around to use .AsReadOnly() IEnumberable&lt;CMS_Page&gt; objects and iterating around them via a foreach call, actually took the time to iterate 10,000 times over an in memory object down by 50 secs from the original test, in comparison to the straight from SQL Server.

So ok, there are much performance gains to be had.  But I will caveat this with, if you are building a website, then using the built in .NET generic List method will save you time coding in the short term, and to go back to the post &quot;right tool right job&quot;?  If you are building a banking app, that will easily be iterating around 10,000,000 rows of a database, and possibly in memory. then the code Dave uses by the looks of it, will save you lots of processing time.  But if you are building a website that is really just a list n detail, then you will save more development time using the built in stuff.

Performance wasn&#039;t my key thought on this post, but what Dave has made me test and therefore find, is there is a good reason which isn&#039;t just “if things were to change due to client requirements” it&#039;s possible massive saving of processing time.

Any other reasons?]]></description>
		<content:encoded><![CDATA[<p>While reading the MVC2.0 book, I noticed the AsReadOnly() method, they returned through as an IList<somethingicantremember> so I came back to my code, set up the methods to IList and tested it.  Not a single bit of difference.  I then put on the .AsReadOnly() when returning the items, and guess what.  The first code example above was 40 secs faster this time around!  I couldn&#8217;t believe what I was seeing so had to do it a few times!!  And that was with:</p>
<p>if(searches is List<sitemap_search>)<br />
                output = (searches as List</sitemap_search><sitemap_search>).FindAll(search => search.Town.Contains(contains));</p>
<p>so I could use the FindAll() method <img src='http://you.arenot.me/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />   However what I will have to give Dave credit for is that he made me try other methods and it turns out FindAll() isn&#8217;t all that efficient.  By changing the code around to use .AsReadOnly() IEnumberable<cms_page> objects and iterating around them via a foreach call, actually took the time to iterate 10,000 times over an in memory object down by 50 secs from the original test, in comparison to the straight from SQL Server.</p>
<p>So ok, there are much performance gains to be had.  But I will caveat this with, if you are building a website, then using the built in .NET generic List method will save you time coding in the short term, and to go back to the post &#8220;right tool right job&#8221;?  If you are building a banking app, that will easily be iterating around 10,000,000 rows of a database, and possibly in memory. then the code Dave uses by the looks of it, will save you lots of processing time.  But if you are building a website that is really just a list n detail, then you will save more development time using the built in stuff.</p>
<p>Performance wasn&#8217;t my key thought on this post, but what Dave has made me test and therefore find, is there is a good reason which isn&#8217;t just “if things were to change due to client requirements” it&#8217;s possible massive saving of processing time.</p>
<p>Any other reasons?</cms_page></sitemap_search></somethingicantremember></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-298</link>
		<dc:creator>Colin</dc:creator>
		<pubDate>Wed, 14 Jul 2010 20:28:31 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-298</guid>
		<description><![CDATA[Can I ask what you mean by &lt;strong&gt;&quot;MS provide some helpers for you, but 99% of the time they miss the point.&quot;&lt;/strong&gt;?

I find the new Generic list built in methods real helpful, and it means you don&#039;t need to write your own versions of it to extend the IList, IEnumerable etc etc, which when you think on it, would mean everyone would need to have access to the DLL with the extension method, or they could just use the List object :)

I do get all the idea of good use of iterfaces, and especially when it comes to DI - the idea of being able to change the logging mechanism of an error handler or a shopping cart is quite nice and lends itself easily to the loosely coupled code.  But I cannot foresee myself changing the database from SQL Server to MySQL without needing to change to PHP :P]]></description>
		<content:encoded><![CDATA[<p>Can I ask what you mean by <strong>&#8220;MS provide some helpers for you, but 99% of the time they miss the point.&#8221;</strong>?</p>
<p>I find the new Generic list built in methods real helpful, and it means you don&#8217;t need to write your own versions of it to extend the IList, IEnumerable etc etc, which when you think on it, would mean everyone would need to have access to the DLL with the extension method, or they could just use the List object <img src='http://you.arenot.me/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I do get all the idea of good use of iterfaces, and especially when it comes to DI &#8211; the idea of being able to change the logging mechanism of an error handler or a shopping cart is quite nice and lends itself easily to the loosely coupled code.  But I cannot foresee myself changing the database from SQL Server to MySQL without needing to change to PHP <img src='http://you.arenot.me/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-297</link>
		<dc:creator>Colin</dc:creator>
		<pubDate>Wed, 14 Jul 2010 20:23:59 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-297</guid>
		<description><![CDATA[&lt;i&gt;&lt;strong&gt;Massive performance loss. Find the boundaries and stick with them.&lt;/strong&gt;&lt;/i&gt;

I did a test tonight.  I have a database table with 7840 towns and villages from different countries around the world.  My code I have set up so that I can save into cache quite easily.

My first test was to iterated around a list in cache 10,000 times to find a random selection with 2 characters in it&#039;s town name:

for (int ii = 0; ii &lt; 10000; ii++)
        {
            searches = new RM_TeacherService().FetchAllSearches();            
            contains = alpha[rnd.Next(0, 25)] + alpha[rnd.Next(0, 25)];
            output = searches.FindAll(search =&gt; search.Town.Contains(contains));
        }

This use the .NET built in FindAll on the objects I retrieved from the database, and then after the first call, the .NET cache object.  It takes on average 123 seconds to run.  I thought that was a bit piss poor really.  I got upset thinking Dave was right again...  So I did this to for his side of the argument:

for (int ii = 0; ii &lt; 10000; ii++)
        {
            contains = alpha[rnd.Next(0, 25)] + alpha[rnd.Next(0, 25)];
            searches = new RM_TeacherService().FetchAllSearches(contains);
        }

Where FetchAllSearches this time takes the 2 character and goes the SQL Database each time, only returning what it gives me.  What made me laugh was on average it was 140 secs to repeate 10,000 times.  So the in memory List was 17 seconds faster (divide that down by the 10,000, each iteration is 0.0017seconds faster.  So really it&#039;s not a performance issue, unless you are doing massive amounts of searches and stuff and for some reason need to hit your database 10,000 real quick.

However this was an extreme case, with 7840 items.  That would be a very unrealistic thing to do, and it is better with data sets that never change, and are real small e.g. lists of Areas in Scotland.  But the difference as seen can be negligable, it&#039;s just a matter of choice if your database server is already getting massively hit.]]></description>
		<content:encoded><![CDATA[<p><i><strong>Massive performance loss. Find the boundaries and stick with them.</strong></i></p>
<p>I did a test tonight.  I have a database table with 7840 towns and villages from different countries around the world.  My code I have set up so that I can save into cache quite easily.</p>
<p>My first test was to iterated around a list in cache 10,000 times to find a random selection with 2 characters in it&#8217;s town name:</p>
<p>for (int ii = 0; ii < 10000; ii++)<br />
        {<br />
            searches = new RM_TeacherService().FetchAllSearches();<br />
            contains = alpha[rnd.Next(0, 25)] + alpha[rnd.Next(0, 25)];<br />
            output = searches.FindAll(search => search.Town.Contains(contains));<br />
        }</p>
<p>This use the .NET built in FindAll on the objects I retrieved from the database, and then after the first call, the .NET cache object.  It takes on average 123 seconds to run.  I thought that was a bit piss poor really.  I got upset thinking Dave was right again&#8230;  So I did this to for his side of the argument:</p>
<p>for (int ii = 0; ii < 10000; ii++)<br />
        {<br />
            contains = alpha[rnd.Next(0, 25)] + alpha[rnd.Next(0, 25)];<br />
            searches = new RM_TeacherService().FetchAllSearches(contains);<br />
        }</p>
<p>Where FetchAllSearches this time takes the 2 character and goes the SQL Database each time, only returning what it gives me.  What made me laugh was on average it was 140 secs to repeate 10,000 times.  So the in memory List was 17 seconds faster (divide that down by the 10,000, each iteration is 0.0017seconds faster.  So really it&#8217;s not a performance issue, unless you are doing massive amounts of searches and stuff and for some reason need to hit your database 10,000 real quick.</p>
<p>However this was an extreme case, with 7840 items.  That would be a very unrealistic thing to do, and it is better with data sets that never change, and are real small e.g. lists of Areas in Scotland.  But the difference as seen can be negligable, it&#8217;s just a matter of choice if your database server is already getting massively hit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave The Ninja</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-296</link>
		<dc:creator>Dave The Ninja</dc:creator>
		<pubDate>Wed, 14 Jul 2010 17:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-296</guid>
		<description><![CDATA[public static IEnumerable Find(this IEnumerable collection, Action action)
{
foreach (var item in collection)
If (action(item))
yield return item;

Your find method would look something like above. However ipnonr ain&#039;t too good for writing code lol. I&#039;ll send u better one tomorrow
}]]></description>
		<content:encoded><![CDATA[<p>public static IEnumerable Find(this IEnumerable collection, Action action)<br />
{<br />
foreach (var item in collection)<br />
If (action(item))<br />
yield return item;</p>
<p>Your find method would look something like above. However ipnonr ain&#8217;t too good for writing code lol. I&#8217;ll send u better one tomorrow<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave The Ninja</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-295</link>
		<dc:creator>Dave The Ninja</dc:creator>
		<pubDate>Wed, 14 Jul 2010 16:55:51 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-295</guid>
		<description><![CDATA[p.s why on earth would you choose filtering on code side to db side?

Massive performance loss. Find the boundaries and stick with them.]]></description>
		<content:encoded><![CDATA[<p>p.s why on earth would you choose filtering on code side to db side?</p>
<p>Massive performance loss. Find the boundaries and stick with them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave The Ninja</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-294</link>
		<dc:creator>Dave The Ninja</dc:creator>
		<pubDate>Wed, 14 Jul 2010 16:53:21 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-294</guid>
		<description><![CDATA[Ok, but not really viable reason coco. 

MS provide some helpers for you, but 99% of the time they miss the point. 

Here we have an example of what I said on the other comment. 



using System;
using System.Collections.Generic;

namespace NinjaTools.Helpers
{
    public static class EnumerableExtensions
    {
        public static IEnumerable Each(this IEnumerable collection, Action action)
        {
            foreach (var item in collection) action(item);
            return collection;
        }
    }
}]]></description>
		<content:encoded><![CDATA[<p>Ok, but not really viable reason coco. </p>
<p>MS provide some helpers for you, but 99% of the time they miss the point. </p>
<p>Here we have an example of what I said on the other comment. </p>
<p>using System;<br />
using System.Collections.Generic;</p>
<p>namespace NinjaTools.Helpers<br />
{<br />
    public static class EnumerableExtensions<br />
    {<br />
        public static IEnumerable Each(this IEnumerable collection, Action action)<br />
        {<br />
            foreach (var item in collection) action(item);<br />
            return collection;<br />
        }<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-293</link>
		<dc:creator>Colin</dc:creator>
		<pubDate>Wed, 14 Jul 2010 16:39:08 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-293</guid>
		<description><![CDATA[But IEnumerable doesn&#039;t have a .ForEach, neither does an IList.  Both of which you need to do something like this:
&lt;/bold&gt;
// find all pages with the title is like &quot;Colin&quot;
IEnumerable&lt;CMS_Page&gt; iPages = new CMS_PageService().FetchAll();
List&lt;CMS_Page&gt; found = new List&lt;CMS_Page&gt;();
foreach (CMS_Page page in pages)
{
        // blah this is baws!!
        if(page.Title.Contains(&quot;Colin&quot;))
             found.Add(page);
}
&lt;/bold&gt;

but with .NET&#039;s generic List object you can do this:

&lt;bold&gt;
// find all pages with the title is like &quot;Colin&quot;
List&lt;CMS_Page&gt; pages = new CMS_PageService().FetchAll();
var tmp = pages.Find(page =&gt; page.Title.Contains(&quot;Colin&quot;));
&lt;/bold&gt;

and that is much much nicer!  No?  So why do I want IList and IEnumberable for all my service classes, when I don&#039;t expose them further than my own websites?]]></description>
		<content:encoded><![CDATA[<p>But IEnumerable doesn&#8217;t have a .ForEach, neither does an IList.  Both of which you need to do something like this:</p>
<p>// find all pages with the title is like &#8220;Colin&#8221;<br />
IEnumerable<cms_page> iPages = new CMS_PageService().FetchAll();<br />
List</cms_page><cms_page> found = new List</cms_page><cms_page>();<br />
foreach (CMS_Page page in pages)<br />
{<br />
        // blah this is baws!!<br />
        if(page.Title.Contains(&#8220;Colin&#8221;))<br />
             found.Add(page);<br />
}</p>
<p>but with .NET&#8217;s generic List object you can do this:</p>
<p><bold><br />
// find all pages with the title is like &#8220;Colin&#8221;<br />
List<cms_page> pages = new CMS_PageService().FetchAll();<br />
var tmp = pages.Find(page => page.Title.Contains(&#8220;Colin&#8221;));<br />
</cms_page></bold></p>
<p>and that is much much nicer!  No?  So why do I want IList and IEnumberable for all my service classes, when I don&#8217;t expose them further than my own websites?</cms_page></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave The Ninja</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-292</link>
		<dc:creator>Dave The Ninja</dc:creator>
		<pubDate>Wed, 14 Jul 2010 16:32:14 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-292</guid>
		<description><![CDATA[P.s if it&#039;s web the right tool right job can be only one thing. Ruby on rails. Nothing else comes close. 

I&#039;ll go over that in the pub in Kowloon too :-)]]></description>
		<content:encoded><![CDATA[<p>P.s if it&#8217;s web the right tool right job can be only one thing. Ruby on rails. Nothing else comes close. </p>
<p>I&#8217;ll go over that in the pub in Kowloon too <img src='http://you.arenot.me/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave The Ninja</title>
		<link>http://you.arenot.me/2010/07/14/asp-net-c-list-vs-ilist/comment-page-1/#comment-291</link>
		<dc:creator>Dave The Ninja</dc:creator>
		<pubDate>Wed, 14 Jul 2010 16:29:51 +0000</pubDate>
		<guid isPermaLink="false">http://you.arenot.me/?p=299#comment-291</guid>
		<description><![CDATA[The .foreach should be coded against Ienumerable. Not ilist ;-)

Anything like array list etc all implement ienumerable at their core

Following me?]]></description>
		<content:encoded><![CDATA[<p>The .foreach should be coded against Ienumerable. Not ilist <img src='http://you.arenot.me/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Anything like array list etc all implement ienumerable at their core</p>
<p>Following me?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
