<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>New Things I Learned</title>
        <link>http://geekswithblogs.net/NewThingsILearned/Default.aspx</link>
        <description> </description>
        <language>en-US</language>
        <copyright>Muljadi Budiman</copyright>
        <managingEditor>muljadibudiman@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>New Things I Learned</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/NewThingsILearned/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Connect to VMWare virtual machines using Remote Desktop</title>
            <category>Windows</category>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/13/connect-to-vmware-virtual-machines-using-remote-desktop.aspx</link>
            <description>&lt;p&gt;Had a short training on &lt;a href="http://www.vmware.com/"&gt;VMWare&lt;/a&gt; on Tuesday, the software development department finally got the official permission (read: get a license) to use &lt;a href="http://www.vmware.com/products/workstation/"&gt;VMWare Workstation&lt;/a&gt;.  I’m no stranger to &lt;a href="http://en.wikipedia.org/wiki/Virtual_machine"&gt;Virtual Machines&lt;/a&gt; (VMs) – started playing with &lt;a href="http://www.microsoft.com/windows/virtual-pc/"&gt;Virtual PC&lt;/a&gt; 2005 a fwe years back and I understood the general concepts of hardware virtualization.  The biggest problem I have with VMs in general is the slowness; I’d rather develop directly on my PC, which is faster.  Can’t say I’ve delved deep into it, but I know enough to utilize it and be dangerous &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.&lt;/p&gt;  &lt;p&gt;Regardless, virtual machines provide a way to simulate multiple computers and I’ve done 3-tier software testing (client to app server using WCF and app server to SQL 2005 backend) to verify our framework can support both 2-tier (client –&amp;gt; DB) and 3-tier deployments.  Rarely used it for development, again due to speed.&lt;/p&gt;  &lt;p&gt;Fast forward to the current time, I’d like to be able to do some coding on &lt;a href="http://www.microsoft.com/windows/windows-7/"&gt;Windows 7&lt;/a&gt;; unfortunately Windows 7 is not quite sanctioned yet to be deployed, and it’s a pain to have to dual-boot.  I do have Windows 7 at home, but got way too many experimental stuff on it &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.  So, I’m setting up a Windows 7 VM so I can do some coding on it.&lt;/p&gt;  &lt;p&gt;Now, nothing wrong with running the VM within VMWare, but I always find that it is a bit sluggish unless I go to full screen mode.  If I do so, it’s a bit of a pain to go back and forth between the host and the VM, and it also means that I have to do it at the host.  Ideally, I’d like to &lt;a href="http://www.microsoft.com/windowsXp/using/mobility/getstarted/Remoteintro.mspx"&gt;remote desktop&lt;/a&gt; into it (for whatever reason, I also feel remoting into a VM session makes for a snappier UI response).&lt;/p&gt;  &lt;p&gt;However, this is not immediately possible to do without some setup; out of the box VMWare provides &lt;a href="http://en.wikipedia.org/wiki/Virtual_Network_Computing"&gt;VNC&lt;/a&gt; connectivity, so you can use that but I am spoiled by remote desktop – it is just way nicer than VNC.  Of course one of the advantages of VNC is that if someone connects to it, the original don’t lose the view of the PC, so different tools for a different job.&lt;/p&gt;  &lt;p&gt;In any case, I’d like to be able to remote desktop into my VMs – there are 2 ways to set this up, one is faster but it is considered as ‘nice’ and may cause problems with typical corporate network setups.  Let’s look at the first approach:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_4.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="554" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_1.png" width="653" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The first approach is to set up the VM setting to have it’s network adapter be a Bridged connection.  This basically sets the VM to use the host’s network card as if it were its own network card.  This also means that the VM will get an IP based on the host network’s settings (usually DHCP-based).  Essentially it brings the network connectivity of the VM to be at the same level as the host.  Now, in a corporate environment (usually domain-based), this may not be allowed, or you may have trouble with accessing the network at all if your VM is not added to the domain.  On a local (home) network, it also means it can communicate with other computers on the same network (good), but if it gets infected with a worm / virus it can also spread to the other computers (bad).&lt;/p&gt;  &lt;p&gt;Depending on your environment, bridging may be fine and if you can do that then by setting it to Bridged mode you can then remote desktop to the VM (you have to remember to turn it on within the VM).&lt;/p&gt;  &lt;p&gt;However, in my current situation, setting a VM network in bridged mode is not condoned and we have to set it to NAT setup.  Each VM that you spun will then get a typical internal IP address (192.168.XXX.XXX); but that means you cannot get to it from the outside.  So what to do?  The answer was provided in &lt;a href="http://communities.vmware.com/thread/106700"&gt;this post&lt;/a&gt; by &lt;a href="http://communities.vmware.com/people/rsa911"&gt;rsa911&lt;/a&gt;– I’m just providing the nice UI snapshots (and also as a reminder for me on how I got it to work in the future).&lt;/p&gt;  &lt;p&gt;In NAT mode, each VM is given an internal IP address and VMWare essentially becomes the bridge between the host and the VMs that runs in it.  Fortunately, you can do &lt;a href="http://en.wikipedia.org/wiki/Port_forwarding"&gt;port forwarding&lt;/a&gt; to these internal IP address.  As detailed &lt;a href="http://en.wikipedia.org/wiki/Remote_Desktop_Protocol"&gt;here&lt;/a&gt;, Remote Desktop uses port 3389 to listen to incoming RDP requests.  So what we need to do is forward an unused port to each VM’s port 3389 and we should be able to remote into them.  Let’s see how that’s done.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_8.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="690" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_3.png" width="621" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;To do port forwarding, we need to access the Virtual Network Editor from VMWare Workstation (under the Edit Menu –&amp;gt; Virtual Network Editor).  The dialog above should show up – select the network adapter that is set to NAT.  Then click on the ‘NAT Settings…’ button, the following dialog will show up:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_10.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="477" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_4.png" width="499" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Click on the ‘Add’ button, and a dialog to map incoming port shows up; the dialog below is filled with the following information:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Listen to port 9997&lt;/li&gt;    &lt;li&gt;Any TCP communication to that port, forward it to the VM with IP of 192.168.118.130 on port 3389 (default RDP port)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_12.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="225" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_5.png" width="360" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;   &lt;/p&gt;&lt;p&gt;&lt;/p&gt;    &lt;p&gt;Tweak it to satisfy your setup – the host port just needs to be an unused port (in this example I use 9997), the VM port needs to be 3389 (unless you’ve changed it) and the VM IP address needs to be the IP address assigned to the VM you’re running (in &lt;a href="http://en.wikipedia.org/wiki/Command_Prompt"&gt;Command Prompt&lt;/a&gt; run &lt;a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ipconfig.mspx?mfr=true"&gt;ipconfig&lt;/a&gt; to easily see this); this is what my output looks like:&lt;/p&gt;    &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_14.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="228" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_6.png" width="684" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;   &lt;p&gt;When done, click OK and the NAT Settings dialog will show up your port forward:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_16.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="263" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_7.png" width="496" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So now I’ve set up VMWare so any incoming request to port 9997 will be forwarded to port 3389 on the VM that has IP of 192.168.118.130.  Remember that for Remote Desktop to work, you still need to make sure your VM has RDP enabled and you have users with passwords added as Remote Users.&lt;/p&gt;  &lt;p&gt;The question is then how do you issue the remote desktop request?  Well, you use the host’s IP (or machine name) as the target, but you provide the port number as well:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_18.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="246" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/ConnecttoVMWarevirtualmachinesusingRemot_DAB4/image_thumb_8.png" width="414" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The VMWare is hosted in my laptop (mbudimane6500) and I’m providing the port RDP should use (9997).  Remember that in a domain environment it’ll try to login as a domain user (DOMAIN_NAME\username format), so if your VM machine is not part of a domain you have to change the username to use to log in.&lt;/p&gt;  &lt;p&gt;If you’ve done everything as mentioned above, now you can remote desktop into your VM machines in a NAT environment &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.  Remember also that you have to &lt;u&gt;do the port forward for every VM&lt;/u&gt; that you wanted to remote into – each one will have a different IP address and each one will need to have a different port number.  Hope this helps others.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136289"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136289" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/136289.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/13/connect-to-vmware-virtual-machines-using-remote-desktop.aspx</guid>
            <pubDate>Fri, 13 Nov 2009 20:42:41 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/136289.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/13/connect-to-vmware-virtual-machines-using-remote-desktop.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/136289.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/136289.aspx</trackback:ping>
        </item>
        <item>
            <title>Make SQLExpress DB accessible from other computers</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/11/make-sqlexpress-db-accessible-from-other-computers.aspx</link>
            <description>&lt;p&gt;When I started using &lt;a href="http://www.microsoft.com/Sqlserver/2005/en/us/express.aspx"&gt;SQLExpress 2005&lt;/a&gt;, everything works on my development machine – however when I wanted to access the database from another machine (trying to access the sqlexpress database remotely) I was not successful.  I discovered the solution way back then, also through &lt;a href="http://www.google.com/search?hl=en&amp;amp;source=hp&amp;amp;q=connect+to+sqlexpress&amp;amp;aq=f&amp;amp;oq=&amp;amp;aqi=g-s1g-sx1g1g-sx6"&gt;Google searches&lt;/a&gt;, which still yield a &lt;a href="http://blogs.msdn.com/sqlexpress/archive/2004/07/23/192044.aspx"&gt;valid result&lt;/a&gt;.  However, every now and then my peers would ask me this same question again.&lt;/p&gt;  &lt;p&gt;Thus I’m creating this blog so I can forward them to this page (instead of talking about it); it’ll also remind me of how to do it if my memory starts to fail me.&lt;/p&gt;  &lt;p&gt;By default, SQLExpress installations does not allow remote connections, so we need to change that first.  To change it, you have to run the &lt;a href="http://technet.microsoft.com/en-us/library/ms173748(SQL.90).aspx"&gt;SQL Server Surface Area Configuration&lt;/a&gt; tool – as the name vaguely implied, this tool will allow you to configure SQL Server’s Surface (what gets exposed).  This tool is installed with your SQL installation (express or not); it should be located in the Microsoft SQL Server 2005 / 2008 program group, under the Configuration Tools subgroup.&lt;/p&gt;  &lt;p&gt;When you run this, the following dialog appears:&lt;/p&gt;  &lt;p&gt; &lt;img title="sac01" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="593" alt="sac01" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/MakeSQLExpressDBaccessiblefromothercompu_D208/sac01_2.png" width="659" border="0" /&gt;&lt;/p&gt;  &lt;p&gt;Select the ‘Surface Area Configuration for Services and Connections’ (as circled above).  The next dialog should appear:&lt;/p&gt;  &lt;p&gt;&lt;img title="sac02" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="514" alt="sac02" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/MakeSQLExpressDBaccessiblefromothercompu_D208/sac02_1.png" width="668" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Select the Remote Connections node on the tree view (as circled above); the dialog changes to the following:&lt;/p&gt;  &lt;p&gt;&lt;img title="sac03" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="515" alt="sac03" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/MakeSQLExpressDBaccessiblefromothercompu_D208/sac03_1.png" width="671" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;As you can see, SQL Express by default does not allow remote connections (the text above it also enforces that); so we’d like to change that – select the ‘Local and remote connections’ radio button, depending on your needs you choose to use TCP/IP or &lt;a href="http://technet.microsoft.com/en-us/library/cc958776.aspx"&gt;named pipes&lt;/a&gt; or both, then click the ‘Apply’ button – an alert should show up telling you that the settings will not take effect until the database engine is restarted, we’ll do this last.&lt;/p&gt;  &lt;p&gt;We’re not finished yet; what happens right now is that the system is set up to allow remote connections.  However, there’s another piece of service that needs to be up before we can connect to it remotely.  That service is the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181087.aspx"&gt;SQL Server Browser&lt;/a&gt; service.  Select the SQL Server Browser node on the left tree view, and the dialog will change to show the following:&lt;/p&gt;  &lt;p&gt;&lt;img title="sac04" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="515" alt="sac04" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/MakeSQLExpressDBaccessiblefromothercompu_D208/sac04_1.png" width="673" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Again, by default the SQLBrowser service is disabled, so let’s change that to Manual, click ‘Apply’ (which will enable the ‘Start’ button) then click the ‘Start’ button, which will start the service.  OK, we’re almost finished; the last step is to restart the SQL Server service itself, so let’s click on the Service node under the SQLEXPRESS and Database Engine node in the tree view.  The dialog will change to the following:&lt;/p&gt;  &lt;p&gt;&lt;img title="sac05" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="514" alt="sac05" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/MakeSQLExpressDBaccessiblefromothercompu_D208/sac05_1.png" width="671" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Click ‘Stop’ and then wait for the service stoppage to complete, then click ‘Start’ again.  You should be able to connect to your SQLExpress instance from another computer (if it’s the default instance, you would use &lt;em&gt;computername&lt;/em&gt;\SQLEXPRESS as the server name).  Good luck!&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136205"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136205" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/136205.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/11/make-sqlexpress-db-accessible-from-other-computers.aspx</guid>
            <pubDate>Wed, 11 Nov 2009 16:08:57 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/136205.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/11/make-sqlexpress-db-accessible-from-other-computers.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/136205.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/136205.aspx</trackback:ping>
        </item>
        <item>
            <title>Putting pictures in Windows Live Writer in their original size</title>
            <category>Blogging</category>
            <category>Personal</category>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/06/putting-pictures-in-windows-live-writer-in-their-original-size.aspx</link>
            <description>&lt;p&gt;I guess I’m one of those people who rarely reads manual, and outside of technical documentation, I rarely read an application’s user manual.  So I’m using &lt;a href="http://windowslivewriter.spaces.live.com/blog/cns!D85741BB5E0BE8AA!174.entry?sa=566237167"&gt;Windows Live Writer&lt;/a&gt;, and I’m trying to embed a picture in my draft blog post.  Well, whenever I do that, the picture always come in scaled down (if it’s even a tad large, like 300 pixels wide), like the picture below:&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="188" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PuttingpicturesinWindowsLiveWriterinthei_D8CD/image_9.png" width="244" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;Well, obviously with technical and text-heavy images, scaling down is bad (can’t read much).  I can go to the HTML source, but I can’t seem to rescale the image, even when I took out the width and height property.  Pretty weird.  So how do I scale it?  Clicking on the image in Live Writer shows me the following information on the right-hand pane:&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="430" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PuttingpicturesinWindowsLiveWriterinthei_D8CD/image_10.png" width="202" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;First off pictures that are inserted into Live Writer automatically gets treated as a clickable link.  If it’s a regular (non-text) picture, most probably that’s desirable; however in most of my cases, I’d like to put in the actual picture itself and don’t want to make it clickable.  So make sure you set the bottom-most combobox (the one under the heading &lt;font color="#0080ff"&gt;Link to&lt;/font&gt;) to None.&lt;/p&gt;  &lt;p&gt;On the top side of this info pane, there are 3 tabs; select the ‘Advanced’ tab and the information displayed changes to:&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="426" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PuttingpicturesinWindowsLiveWriterinthei_D8CD/image_13.png" width="199" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;The combobox under the &lt;font color="#0080ff"&gt;Size&lt;/font&gt; heading will also list the original size; select that and your picture now gets shown in its original size.  I’m happy (&lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;)to have found this since in some of my past blog posts I actually had to set the width/height of the picture manually in the HTML.&lt;/p&gt;  &lt;p&gt;Also, with &lt;a href="http://geekswithblogs.net/"&gt;geekswithblogs&lt;/a&gt;, to set the tags to associate with your blog post, the bottom part of Live Writer has this ‘Set categories’ combobox – click on it and it’ll show you the tags that you’ve defined in your blog:&lt;/p&gt;  &lt;p&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="346" alt="image" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PuttingpicturesinWindowsLiveWriterinthei_D8CD/image_16.png" width="592" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136068"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136068" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/136068.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/06/putting-pictures-in-windows-live-writer-in-their-original-size.aspx</guid>
            <pubDate>Fri, 06 Nov 2009 21:31:34 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/136068.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/06/putting-pictures-in-windows-live-writer-in-their-original-size.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/136068.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/136068.aspx</trackback:ping>
        </item>
        <item>
            <title>My Coding Helpers: Verify exception thrown in Unit Test</title>
            <category>.NET</category>
            <category>Personal</category>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/05/my-coding-helpers-verify-exception-thrown-in-unit-test.aspx</link>
            <description>&lt;p&gt;Every now and then, I end up writing and re-writing the same code (or very, very similar) to achieve a functionality that I’ve had before.  Sometimes I can remember where I wrote it, so I can just copy and paste.  At other times, I have to redo it again since I forgot where it was.  So I’d like to put it up on my blog so I have access to it.  If anyone else gets to see it and can use it, so much the better for everyone.  Of course, I’d also accept criticism to the code too &lt;img alt="" src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.&lt;/p&gt;
&lt;p&gt;So, this is my first one; because I’ve used it for awhile and I ended up having to rewrite it yet again today for a pet project that I have.  Now, I understand not everyone likes to do &lt;a href="http://en.wikipedia.org/wiki/Unit_testing"&gt;Unit Testing&lt;/a&gt;, but I enjoy it every now and then.  It is somewhat boring &amp;amp; tedious, but it also gives me some relaxation since I can do very, very simple code and don’t have to think too much – unit test code should not be very complex.  In most unit tests, the code will try to go through the path of the tested code and usually try to handle the happy path.  I don’t do pure &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;Test Driven Development&lt;/a&gt;, but I understood some benefit of what TDD can do.&lt;/p&gt;
&lt;p&gt;One advantage of doing TDD is that you will end up also writing tests (and code) that will verify arguments.  And since your code has argument checks, then your unit tests also have to verify both success and failure cases of those arguments.  Consider the following very simple code:&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass
&lt;/span&gt;{
   &lt;span style="COLOR: blue"&gt;public double &lt;/span&gt;SR(&lt;span style="COLOR: blue"&gt;double &lt;/span&gt;d)
   {
      &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;Math&lt;/span&gt;.Sqrt(d);
   }
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Essentially you just wanted a wrapper method, instead of calling the function &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="COLOR: #2b91af"&gt;Math&lt;/span&gt;.Sqrt()&lt;/font&gt;&lt;/font&gt; all the time; simple enough.  But let’s say that you also wanted to change the default behavior; such that the consuming code should never ever actually call this method with a negative number.  If it is a negative number, then you wanted to throw an exception.  Easy enough to do…&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public double &lt;/span&gt;SR(&lt;span style="COLOR: blue"&gt;double &lt;/span&gt;d)
{
   &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(d &amp;lt; 0)
      &lt;span style="COLOR: blue"&gt;throw new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;"Argument can't be negative!"&lt;/span&gt;);

   &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;Math&lt;/span&gt;.Sqrt(d);
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;As you can see from the code above, the code is still trivial.  Of course, now you want to write a unit test for it; there are 2 unit tests you have to create: the successful case and the failure case.  So let’s see what the unit tests would look like the following (I’m using Visual Studio’s &lt;a href="http://msdn.microsoft.com/en-us/library/ms379625(VS.80).aspx"&gt;unit testing framework&lt;/a&gt;):&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;SRTest_Success()
{
   &lt;span style="COLOR: #2b91af"&gt;SomeClass &lt;/span&gt;sc = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass&lt;/span&gt;();
   &lt;span style="COLOR: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(sc.SR(100), 10.0);
}

[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;SRTest_Failure()
{
   &lt;span style="COLOR: #2b91af"&gt;SomeClass &lt;/span&gt;sc = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass&lt;/span&gt;();
   &lt;span style="COLOR: blue"&gt;try
   &lt;/span&gt;{
      sc.SR(-100);
      &lt;span style="COLOR: #2b91af"&gt;Assert&lt;/span&gt;.Fail();
   }
   &lt;span style="COLOR: blue"&gt;catch &lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;)
   {
   }
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Again, simple, very trivial methods – however the failure case is rather verbose; you have to create a &lt;a href="http://msdn.microsoft.com/en-us/library/0yd65esw.aspx"&gt;try-catch&lt;/a&gt; block (an empty catch at that too), so another way to do it is to use the &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx"&gt;ExpectedExceptionAttribute&lt;/a&gt;.&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
[&lt;span style="COLOR: #2b91af"&gt;ExpectedException&lt;/span&gt;(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;))]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;SRTest_Failure()
{
   &lt;span style="COLOR: #2b91af"&gt;SomeClass &lt;/span&gt;sc = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass&lt;/span&gt;();
   sc.SR(-100);
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The problem with the above approach is that if you want to be able to catch multiple exceptions (not recommended in creating unit tests, but some people may want to cut corners &lt;img alt="" src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;), you can’t do it – you can’t apply multiple ExpectedException attributes.  Another problem is if your unit test code is rather large, it may have other code paths where there’s an actual failure that will throw an &lt;a href="http://msdn.microsoft.com/en-us/library/system.argumentexception.aspx"&gt;ArgumentException&lt;/a&gt; which will pass the test happily but will result in a false positive.&lt;/p&gt;
&lt;p&gt;So, I needed a way to be able to catch exceptions in the code itself, but I don’t want to write try-catch blocks all over the place.  So I came up with the following helper method to help me verify exceptions are thrown:&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;UnitTestHelper
&lt;/span&gt;{
   &lt;span style="COLOR: green"&gt;// Method accepts an action delegate, and a generic Exception type
   &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public static void &lt;/span&gt;VerifyExceptionThrown&amp;lt;TException&amp;gt;(&lt;span style="COLOR: #2b91af"&gt;Action &lt;/span&gt;action)
      &lt;span style="COLOR: blue"&gt;where &lt;/span&gt;TException : &lt;span style="COLOR: #2b91af"&gt;Exception
   &lt;/span&gt;{
      &lt;span style="COLOR: green"&gt;// Verify arguments - action can't be null
      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(action == &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;)
         &lt;span style="COLOR: blue"&gt;throw new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;"Action to test cannot be null!"&lt;/span&gt;);

      &lt;span style="COLOR: green"&gt;// Verify arguments - TException cannot be of type Exception
      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(TException).Equals(&lt;span style="COLOR: blue"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;Exception&lt;/span&gt;)))
         &lt;span style="COLOR: blue"&gt;throw new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;"TException type cannot be of type Exception"&lt;/span&gt;);

      &lt;span style="COLOR: green"&gt;// Actually call it
      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;try
      &lt;/span&gt;{
         action();
      }
      &lt;span style="COLOR: blue"&gt;catch &lt;/span&gt;(TException)
      {
         &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;
      }

      &lt;span style="COLOR: green"&gt;// If it gets here, fail the test
      &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;Assert&lt;/span&gt;.Fail(&lt;span style="COLOR: #a31515"&gt;"Action does not throw the exception expected"&lt;/span&gt;);
   }
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The code above is fairly simple; the helper method accepts an &lt;a href="http://msdn.microsoft.com/en-us/library/system.action.aspx"&gt;Action&lt;/a&gt; delegate as a parameter and a generic TException type to be passed and I put in a &lt;a href="http://msdn.microsoft.com/en-us/library/d5x73970.aspx"&gt;constraint&lt;/a&gt; that has to derive from &lt;a href="http://msdn.microsoft.com/en-us/library/system.exception.aspx"&gt;Exception&lt;/a&gt; class.  To also be nice and proper, the method also checks its parameters: the &lt;font size="2"&gt;&lt;font face="Courier New"&gt;action &lt;/font&gt;&lt;/font&gt;parameter can’t be null, and the generic &lt;font size="2"&gt;&lt;font face="Courier New"&gt;TException &lt;/font&gt;&lt;/font&gt;type cannot be of type Exception itself (because it’ll catch all exceptions, which won’t prove/disprove the unit test itself, but modify it as you want).  At the end, it’ll fail the unit test, since it should never hit the &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="COLOR: #2b91af"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.fail.aspx"&gt;Assert.Fail&lt;/a&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt; method call.&lt;/p&gt;
&lt;p&gt;The unit test will look like the following:&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;SRTest_Failure()
{
   &lt;span style="COLOR: #2b91af"&gt;SomeClass &lt;/span&gt;sc = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass&lt;/span&gt;();
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(() =&amp;gt; sc.SR(-100));
}

[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;SRTest_Failure_AnonymousDelegate()
{
   &lt;span style="COLOR: #2b91af"&gt;SomeClass &lt;/span&gt;sc = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;SomeClass&lt;/span&gt;();
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(&lt;span style="COLOR: blue"&gt;delegate&lt;/span&gt;() { sc.SR(-100); });
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I put in 2 methods – the first one uses &lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;Lambda Expressions&lt;/a&gt;, which may not be used by those that are still using strictly .NET 2.0 framework only, so the second one demonstrates the same call but using &lt;a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx"&gt;anonymous methods&lt;/a&gt;.  If you’re strictly using .NET 2.0, the Action delegate is not available in the .NET Framework – you have to create your own.  &lt;/p&gt;
&lt;p&gt;After understanding simple Lambda Expressions, personally for me, the unit test code is more readable, since I don’t need to look at any attributes, and the unit test code can contain further verification of exceptions thrown and also it can have further asserts.&lt;/p&gt;
&lt;p&gt;For completeness, the code below is the unit test that tests the VerifyExceptionThrown method.  The code demonstrates the ‘eating your own dog food’ mentality: I get to use the method that I’m unit testing itself.&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;VerifyExceptionThrown_TestFailure_InvalidExceptionType()
{
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(() =&amp;gt;
      &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Exception&lt;/span&gt;&amp;gt;(() =&amp;gt; { }));
}

[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;VerifyExceptionThrown_TestFailure_NullAction()
{
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ArgumentException&lt;/span&gt;&amp;gt;(() =&amp;gt;
      &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;DivideByZeroException&lt;/span&gt;&amp;gt;(&lt;span style="COLOR: blue"&gt;null&lt;/span&gt;));
}

[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;VerifyExceptionThrown_TestFailure_ExceptionNotThrown()
{
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;AssertFailedException&lt;/span&gt;&amp;gt;(() =&amp;gt;
      &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;DivideByZeroException&lt;/span&gt;&amp;gt;(() =&amp;gt; &lt;span style="COLOR: #2b91af"&gt;Debug&lt;/span&gt;.Assert(&lt;span style="COLOR: blue"&gt;true&lt;/span&gt;)));
}

[&lt;span style="COLOR: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="COLOR: blue"&gt;public void &lt;/span&gt;VerifyExceptionThrown_TestSuccess_DivideByZeroException()
{
   &lt;span style="COLOR: blue"&gt;int &lt;/span&gt;i = 0;
   &lt;span style="COLOR: #2b91af"&gt;UnitTestHelper&lt;/span&gt;.VerifyExceptionThrown&amp;lt;&lt;span style="COLOR: #2b91af"&gt;DivideByZeroException&lt;/span&gt;&amp;gt;(() =&amp;gt; i = 5 / i);
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136039"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=136039" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/136039.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/05/my-coding-helpers-verify-exception-thrown-in-unit-test.aspx</guid>
            <pubDate>Thu, 05 Nov 2009 21:22:35 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/136039.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/11/05/my-coding-helpers-verify-exception-thrown-in-unit-test.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/136039.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/136039.aspx</trackback:ping>
        </item>
        <item>
            <title>Coders 4 Charities: Epilogue</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/21/coders-4-charities-epilogue.aspx</link>
            <description>&lt;p&gt;I was involved in the &lt;a href="http://c4c-stl.org"&gt;St. Louis Coders 4 Charities&lt;/a&gt; event this past weekend; I was part of the &lt;a href="http://www.turningpointdvs.com/AboutUs/AboutWebsite.aspx"&gt;TURNERS team&lt;/a&gt; that is responsible with helping the &lt;a href="http://www.turningpointdvs.com/"&gt;Turning Point&lt;/a&gt; agency (Warren County Council Against Domestic Violence) create a website.  You see, out of the 9 charitable organizations in C4C’s list, Turning Point was the only one that did not have a website.&lt;/p&gt;  &lt;p&gt;It was an interesting project, and it allowed me to learn quite a bit of stuff.  We recommended &lt;a href="http://www.webhost4life.com/"&gt;Webhost4life&lt;/a&gt; to be their webhost provider, asked them to register a domain name of their preference (&lt;a href="http://www.turningpointdvs.com"&gt;www.turningpointdvs.com&lt;/a&gt;) , and we decided to use &lt;a href="http://www.sitefinity.com"&gt;SiteFinity&lt;/a&gt; as the CMS for their site.  All these legwork were done before the starting time on Friday, October 16th.&lt;/p&gt;  &lt;p&gt;On the day of the event, we had a great turnout: over 50 volunteers showed up on that first night, of various expertises and skill levels.  &lt;a href="http://geekswithblogs.net/kgrossnicklaus/Default.aspx"&gt;Kevin Grossnicklaus&lt;/a&gt; gave an opening statement and then teams were introduced and proceeded to get together with the agency’s contact person to discuss about the project to be done.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.assembla.com/"&gt;Assembla.com&lt;/a&gt; was gracious enough to donate to C4C a web workspace, which allows for bug tracking, source control (SubVersion using &lt;a href="http://tortoisesvn.tigris.org/"&gt;Tortoise&lt;/a&gt; and &lt;a href="http://www.visualsvn.com/"&gt;VisualSVN&lt;/a&gt;), team membership registration and file sharing of project assets.  Most of Friday was used for familiarizing ourselves with the tools available and digging a bit deeper into the requirements side that each project needed.&lt;/p&gt;  &lt;p&gt;My team had these great individuals as members:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.facebook.com/profile.php?id=1363487253&amp;amp;ref=ts"&gt;Brian Bogovich&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.facebook.com/profile.php?id=100000163057219&amp;amp;ref=search&amp;amp;sid=1523292748.1488377875..1"&gt;Chandan Gaddam&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Gary Sheldon &lt;/li&gt;    &lt;li&gt;&lt;a href="http://jay.tuley.name/"&gt;Jay Tuley&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.linkedin.com/in/jeffclifton"&gt;Jeffrey Clifton&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.facebook.com/profile.php?id=36106449"&gt;John Clifton&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It was a great experience for me altogether; it was amazing to see these volunteers (mostly developers, but we also have Project Managers, Testers, Graphics Artists, etc.) work until very late night, all for these charity organizations, with no rewards whatsoever.  It is very, very eye-opening.  We closed up on Friday around midnight, opened up again on Saturday from 7:30 AM, again all the way to around 11:30 PM.  &lt;/p&gt;  &lt;p&gt;We had a closing show-and-tell session on 5 PM Sunday; all of the projects are considered done in principal: some needed to still be uploaded to the webhost, some needed a bit of polish here and there, but the main requests of the project are done.  The place was closed around 7:30 PM and everyone went home tired, but elated – it was also evident that people needed a good night’s sleep afterwards.&lt;/p&gt;  &lt;p&gt;It was a great event, it benefitted quite a bit of the Greater St. Louis charity organizations, and I am overwhelmed by the outpouring of support from the St. Louis IT community.  I’m sure we’ll have another one next year, so hopefully it’ll be even bigger and better than this time around &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.&lt;/p&gt;  &lt;p&gt;Pictures of the event can be found &lt;a href="http://www.flickr.com/groups/c4cstl/pool/show/"&gt;here&lt;/a&gt;.  I still need to upload my pics, but I’ll add it to the pool.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135641"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135641" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/135641.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/21/coders-4-charities-epilogue.aspx</guid>
            <pubDate>Wed, 21 Oct 2009 20:01:51 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/135641.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/21/coders-4-charities-epilogue.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/135641.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/135641.aspx</trackback:ping>
        </item>
        <item>
            <title>Coders 4 Charities &amp;ndash; St. Louis: October 16th - 18th</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/02/coders-4-charities-ndash-st.-louis-october-16th---18th.aspx</link>
            <description>&lt;p&gt;Kansas City held this great event where developers volunteer their time to fulfill the IT needs of charitable organizations back in &lt;a href="http://coders4charities.org/pastevents/"&gt;April 2008&lt;/a&gt;.  The event was called &lt;a href="http://coders4charities.org/"&gt;Coders 4 Charities&lt;/a&gt; and it was a great success.  It was then made into an annual event, with their last one held last April.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/kgrossnicklaus"&gt;Kevin Grossnicklaus&lt;/a&gt; heard about this and he worked with other community leaders in organizing a similar event to be held in St. Louis, thus &lt;a href="http://c4c-stl.org/"&gt;C4C – St. Louis&lt;/a&gt; is born.  I volunteered to be part of this great community effort.  &lt;/p&gt;  &lt;p&gt;C4C – St. Louis is still looking for a few more additional volunteers, the more the merrier, right?  If you’re interested in volunteering some of your time and technological skills &amp;amp; knowledge to help a local charitable organization, please go to the website and &lt;a href="http://c4c-stl.org/Volunteer.aspx"&gt;register&lt;/a&gt;.  In fact, they’re also currently looking for a couple more people to step up and assume a Team Lead role.  There’s also a &lt;a href="http://c4c-stl.org/FAQ.aspx"&gt;FAQ page&lt;/a&gt; if you have any questions.&lt;/p&gt;  &lt;p&gt;Washington University’s Center for the Application of Information Technology (&lt;a href="http://www.cait.wustl.edu/"&gt;CAIT&lt;/a&gt;) has graciously provided the venue for this event; they’re located &lt;a href="http://maps.google.com/maps?f=q&amp;amp;source=s_q&amp;amp;hl=en&amp;amp;geocode=&amp;amp;q=cait,%20st.%20louis&amp;amp;sll=37.0625,-95.677068&amp;amp;sspn=55.937499,114.169922&amp;amp;ie=UTF8&amp;amp;hq=cait,&amp;amp;hnear=Saint%20Louis,%20MO&amp;amp;ll=38.649858,-90.330105&amp;amp;spn=0.013641,0.027874&amp;amp;z=16&amp;amp;iwloc=A"&gt;here&lt;/a&gt;.  The event will start on late Friday afternoon  on October 16th and will continue until the evening of October 18th; food, soda and coffee will be provided throughout the effort.  More information can be read at the &lt;a href="http://c4c-stl.org"&gt;C4C – St. Louis website&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I look forward to meet other volunteers at this charitable event.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135249"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135249" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/135249.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/02/coders-4-charities-ndash-st.-louis-october-16th---18th.aspx</guid>
            <pubDate>Fri, 02 Oct 2009 16:15:59 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/135249.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/10/02/coders-4-charities-ndash-st.-louis-october-16th---18th.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/135249.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/135249.aspx</trackback:ping>
        </item>
        <item>
            <title>Covariance in .NET 4.0</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/30/covariance-in-.net-4.0.aspx</link>
            <description>&lt;p&gt;On Monday I presented at the &lt;a href="http://stlnet.org"&gt;St. Louis .NET User Group&lt;/a&gt;, and the topic was ‘What’s new in VS2010 and .NET 4.0 Framework’.  Within the whirlwind look at all the new and cool stuff coming in the near future, the subject of &lt;a href="http://msdn.microsoft.com/en-us/library/dd233054(VS.100).aspx"&gt;Covariance and Contravariance&lt;/a&gt; support was discussed.  Given the time constraint I only spent 3-4 minutes at it.  I alluded to the group that the concept of Covariance/Contravariance is rather hard to understand, because it is not as intuitive as it could be; I just asked them to trust me that the support is there.&lt;/p&gt;  &lt;p&gt;After the session, talking with some people, apparently the concept is REALLY hard to understand; and in hindsight I should’ve known this too.  It took me a good 3-4 hours of fiddling around and research to get the concept embedded in my head so it will take others the same amount of time to understand it too.&lt;/p&gt;  &lt;p&gt;Given that the concept is rather hard, I thought I should blog about it so I don’t forget it in the future &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.  In this post, I’ll talk about Covariance only and I’ll post about Contravariance in the future.&lt;/p&gt;  &lt;p&gt;Let’s start at the very, very basic level: the word itself.  Variance.  From &lt;a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)"&gt;this article on Wikipedia&lt;/a&gt;, I managed to synthesize that the word variance means ‘conversion of types’.  Covariance is the conversion of a type from more specific to more general.  Contravariance denotes the reverse: conversion of a type from more general to more specific.&lt;/p&gt;  &lt;p&gt;Let’s do a simple case:&lt;/p&gt;  &lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="600"&gt;         &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test()
{
   &lt;span style="color: blue"&gt;string &lt;/span&gt;str = &lt;span style="color: #a31515"&gt;"Test"&lt;/span&gt;;
   ProcessObject(str);
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;ProcessObject(&lt;span style="color: blue"&gt;object &lt;/span&gt;obj)
{
   &lt;span style="color: green"&gt;// Some code...
&lt;/span&gt;}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In the code above we have a simple case where the consuming code declares a &lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;string&lt;/font&gt;&lt;/span&gt;, and then passes the &lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;string &lt;/font&gt;&lt;/span&gt;into a method that accepts an &lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;object&lt;/font&gt;&lt;/span&gt;.  That is Covariance in action: conversion of a type from a more specific one to a more general one (&lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;string &lt;/font&gt;&lt;/span&gt;to &lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;object&lt;/font&gt;&lt;/span&gt;).  Of course, the term doesn’t pop into mind; as developers we just see that the operation is valid since we’re passing a variable to a method that can accept the base type.  So, to go 1 step further, the following is also valid:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Base
&lt;/span&gt;{
}

&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Derived &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Base
&lt;/span&gt;{
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test2()
{
   &lt;span style="color: #2b91af"&gt;Derived &lt;/span&gt;d = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Derived&lt;/span&gt;();
   ProcessBase(d);
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;ProcessBase(&lt;span style="color: #2b91af"&gt;Base &lt;/span&gt;b)
{
   &lt;span style="color: green"&gt;// Some code...
&lt;/span&gt;}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;As you can see, this is simple OOP in action; we’re passing an instance of the Derived class (a more specific type) to a method that can accept the Base class (a more general type).  Everything is logical, and makes sense.  Covariance in action.&lt;/p&gt;

&lt;p&gt;So let’s push further 1 more step; we’re going to do covariance (conversion of type from more specific to more general), but without methods this time.&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test3()
{
   &lt;span style="color: blue"&gt;string&lt;/span&gt;[] strs = &lt;span style="color: blue"&gt;new string&lt;/span&gt;[5];
   &lt;span style="color: blue"&gt;object&lt;/span&gt;[] objs = strs;

   objs[0] = &lt;span style="color: #a31515"&gt;"Test"&lt;/span&gt;;
   objs[1] = 7;  &lt;span style="color: green"&gt;// This will crash the application
&lt;/span&gt;}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;In the above code, this conversion of arrays is fully legal; however the last line will cause the program to crash.  The array conversion from &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;[]&lt;/font&gt;&lt;/font&gt; to &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;[]&lt;/font&gt;&lt;/font&gt; is not very common, but it does work.  However, since you can also use the array to assign values, this conversion is considered to be an &lt;em&gt;unsafe covariance&lt;/em&gt;, since it will only work for valid assignment, but it cannot be checked at compile-time.&lt;/p&gt;

&lt;p&gt;So, let’s take it 1 step further again:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test4()
{
   &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; strs = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();
   &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; objs = strs;  &lt;span style="color: green"&gt;// Why not?

   // To prevent the following code:
   &lt;/span&gt;objs.Add(5);
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The above code won’t compile, the second line ( &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; objs = strs;&lt;/font&gt;&lt;/font&gt; ) will fail with the error that it cannot implicitly convert &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; to &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; – it’s how it has been in .NET.  The initial question is why is this not allowed?  It’s because if we’re allowing the conversion, then the line afterwards (which is very illegal and we don’t want to have that kind of code ever) will crash the application definitely, and it’s not something that can be checked during compile-time.  Very similar to the prior example, but developers will use generics more so than arrays, thus this is not allowed.&lt;/p&gt;

&lt;p&gt;In both cases, the application will break because both constructs (&lt;a href="http://msdn.microsoft.com/en-us/library/system.array.aspx"&gt;array&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/a&gt;) allows assigning of values into the container.  But what if we don’t allow assigning values?  Let’s see if we can create such a scenario:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt;
{
   T &lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;int &lt;/span&gt;index] { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
}

&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NewList&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt;
{
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test5()
{
   &lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; strs = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;NewList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();
   &lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; objs = strs;  &lt;span style="color: green"&gt;// This should now be valid!

   // Can't assign to objs, can only get value out
   &lt;/span&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;temp = objs[0];
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;So we’re trying to create a construct just for our own use here: we have an interface (&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/font&gt;) that provides a get-only indexer.  We need to have this interface implemented, so we created the &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;NewList&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/font&gt; class which inherits from &lt;a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/a&gt; and also implements &lt;font color="#393925"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/font&gt;.  In the above code (which still won’t compile due to the assignment of &lt;font face="Courier New" size="2"&gt;strs&lt;/font&gt; to &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;), we’re showing that with the &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt; &lt;/font&gt;&lt;/font&gt;interface, the code cannot make changes to the container; as such it should be legal.  There’s no way to write code to break the application now that’s not detectable at compile time.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;That’s exactly the argument as to why this type of covariance should be allowed.  In &lt;a href="http://msdn.microsoft.com/en-us/library/bb383815(VS.100).aspx"&gt;.NET 4.0&lt;/a&gt;, this is now possible:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If the construct only allows getting the values out only &lt;/li&gt;

  &lt;li&gt;And the construct does not allow changing the values contained within &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code above will be legal in .NET 4.0, with the following minor change:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;out &lt;/span&gt;T&amp;gt;
{
   T &lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;int &lt;/span&gt;index] { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The change is in the signature of the generic type &lt;font color="#393925"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;T&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;, we provide an &lt;a href="http://msdn.microsoft.com/en-us/library/dd469487(VS.100).aspx"&gt;out&lt;/a&gt; keyword.  That &lt;a href="http://msdn.microsoft.com/en-us/library/dd469487(VS.100).aspx"&gt;out&lt;/a&gt; keyword tells the compiler that the interface (or class) will only use the generic type &lt;font color="#393925"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;T&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; as output, but never as input.  This allows the compiler to verify that the interface is indeed conforming to that specification (&lt;font color="#393925"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;T&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; is never used as input) and it will then allow covariance for &lt;font color="#393925"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IOutOnlyList&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt; from a more specific type (&lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;string&lt;/font&gt;&lt;/span&gt;) to a more generic type (&lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;object&lt;/font&gt;&lt;/span&gt;).&lt;/p&gt;

&lt;p&gt;The example so far is for our own interfaces; to allow covariance .NET 4.0 changes the following constructs (by using the &lt;span style="color: blue"&gt;&lt;font face="Courier New" size="2"&gt;out &lt;/font&gt;&lt;/span&gt;keyword):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0(VS.100).aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/78dfe2yb(VS.100).aspx"&gt;IEnumerator&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb351562(VS.100).aspx"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb344977(VS.100).aspx"&gt;IGrouping&amp;lt;TKey, TElement&amp;gt;&lt;/a&gt; (Covariant for TKey)&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/kt456a2y(VS.100).aspx"&gt;Converter&lt;/a&gt; delegate (Covariant for the output, Contravariant for the input)&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb534960(VS.100).aspx"&gt;Func&lt;/a&gt; delegates from the &lt;a href="http://msdn.microsoft.com/en-us/library/system(VS.100).aspx"&gt;System&lt;/a&gt; namespace (Covariant for the output types only)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of these changes, the code below will now work; you can assign variables of type &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; to type &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;. Since the conversion works, you can also use this to pass in parameters into methods as well.&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Test6()
{
   &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; strs = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();
   &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; objs = strs;  &lt;span style="color: green"&gt;// IEnumerable can't change values contained

   &lt;/span&gt;ProcessObjects(strs);
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;ProcessObjects(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; objs)
{
   &lt;span style="color: green"&gt;// Do something with objs
&lt;/span&gt;}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;That’s the idea of Covariance in .NET 4.0 and how it is used.  Contravariance is the reverse of Covariance, but the explanation takes a bit longer…&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135194"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135194" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/135194.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/30/covariance-in-.net-4.0.aspx</guid>
            <pubDate>Wed, 30 Sep 2009 19:45:10 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/135194.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/30/covariance-in-.net-4.0.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/135194.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/135194.aspx</trackback:ping>
        </item>
        <item>
            <title>Presenting for the St. Louis .NET User Group &amp;ndash; What&amp;rsquo;s new in VS2010 and .NET 4.0</title>
            <category>.NET</category>
            <category>Windows</category>
            <category>WPF</category>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/29/presenting-for-the-st.-louis-.net-user-group-ndash-whatrsquos.aspx</link>
            <description>&lt;p&gt;Last night I had the pleasure of presenting a session on ‘What’s new in VS2010 and .NET 4.0’ for the &lt;a href="http://stlnet.org"&gt;St. Louis .NET User Group&lt;/a&gt;.  Got to meet with lots of people there, so greets go to all &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.  I had fun with the session; I liked talking about new technology and it’s pretty neat to see everyone and just mingle around with people in general.  Pizza was great (thanks to last night’s sponsor: &lt;a href="http://www.quilogy.com/"&gt;Quilogy&lt;/a&gt;) – I have to also thank &lt;a href="http://geekswithblogs.net/SSpradlin"&gt;Scott Spradlin&lt;/a&gt; for allowing me to present to the group.&lt;/p&gt;  &lt;p&gt;The session itself has the same presentation slide as my .NET 4.0 session which I presented in &lt;a href="http://stlouisdayofdotnet.com/"&gt;St. Louis Day of .NET&lt;/a&gt;, which I’ve made available as detailed in &lt;a href="http://geekswithblogs.net/NewThingsILearned/archive/2009/09/04/.net-4.0-and-c-4.0-sessions-materials.aspx"&gt;this post&lt;/a&gt;.  I did make a project that tries to also demonstrate some of the new constructs, which I didn’t have time to do when I first presented it.  So &lt;a href="http://sites.google.com/site/newthingsilearned/zipfiles/NewStuff.zip?attredirects=0"&gt;here it is&lt;/a&gt;.  It is a simple WinForms application, with 6 buttons, each of which demonstrates:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Using the &lt;a href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx"&gt;dynamic&lt;/a&gt; keyword to invoke a method, and also to provide a generic Add method which accepts 2 dynamic parameters &lt;/li&gt;    &lt;li&gt;Demonstrates support for consuming COM-Interop methods (opening up a Word document).  An MSDN howto article on this is available &lt;a href="http://msdn.microsoft.com/en-us/library/dd264733(VS.100).aspx"&gt;here&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(VS.100).aspx"&gt;BigInteger&lt;/a&gt; structure &lt;/li&gt;    &lt;li&gt;Using the &lt;a href="http://msdn.microsoft.com/en-us/library/dd383325(VS.100).aspx"&gt;Tuples&lt;/a&gt; constructs &lt;/li&gt;    &lt;li&gt;Demonstrate &lt;a href="http://msdn.microsoft.com/en-us/library/dd381935(VS.100).aspx"&gt;ConcurrentLinkedList&amp;lt;T&amp;gt;&lt;/a&gt; (thread-safe collections are in the &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.concurrent(VS.100).aspx"&gt;System.Collections.Concurrent namespace&lt;/a&gt;) &lt;/li&gt;    &lt;li&gt;Demonstrate the use of &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialogcustomplace(VS.100).aspx"&gt;FileDialogCustomPlace&lt;/a&gt; to add application-specific one-click shortcuts &lt;/li&gt;    &lt;li&gt;&lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PresentingfortheSt.Louis.NETUserGroupW.0_ED2B/openfiledialog.png"&gt;&lt;img title="openfiledialog" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" alt="openfiledialog" src="http://geekswithblogs.net/images/geekswithblogs_net/NewThingsILearned/WindowsLiveWriter/PresentingfortheSt.Louis.NETUserGroupW.0_ED2B/openfiledialog_thumb.png" border="0" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;It also has other files that demonstrates other features; the variance.cs file contains constructs that shows &lt;a href="http://msdn.microsoft.com/en-us/library/dd233054(VS.100).aspx"&gt;Covariance and Contravariance&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I hope this may be of use to others.&lt;/p&gt;  &lt;p&gt;On a side note, based on the recommendation of speakers at the St. Louis Day of .NET, I’ve created a Twitter account: &lt;a href="http://twitter.com/mulbud"&gt;mulbud&lt;/a&gt;.  Hopefully I can contribute to Twitterverse in a positive way &lt;img src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135166"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135166" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/135166.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/29/presenting-for-the-st.-louis-.net-user-group-ndash-whatrsquos.aspx</guid>
            <pubDate>Tue, 29 Sep 2009 21:53:51 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/135166.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/29/presenting-for-the-st.-louis-.net-user-group-ndash-whatrsquos.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/135166.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/135166.aspx</trackback:ping>
        </item>
        <item>
            <title>Linq == Deferred Execution == Always Re-execute</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/23/linq--deferred-execution--always-re-execute.aspx</link>
            <description>&lt;p&gt;Got bit by a bug caused by me trying to do stuff smartly in Linq.  Gentle reminder that Linq expressions are always recalculated.  The bit of code is the following:&lt;/p&gt;
&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="600"&gt;
            &lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public static void &lt;/span&gt;AddKnownTypes(&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;&amp;gt; knownTypes)
{
   &lt;span style="COLOR: green"&gt;// We only want to collect types that are new (hasn't been registered yet)
   &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;Type&lt;/span&gt;&amp;gt; newTypes = knownTypes.Where(t =&amp;gt; !_knownTypes.Contains(t));
   _knownTypes.AddRange(newTypes);

   &lt;span style="COLOR: green"&gt;// Fill in a dictionary of these newly added types
   &lt;/span&gt;&lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: #2b91af"&gt;Type &lt;/span&gt;type &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;newTypes)
   {
      _types[type.Name] = type;
   }
}&lt;/pre&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Looking at it, the code seems to be fairly benign; the method accepts a list of types, we only wanted the new ones, so filter using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx"&gt;Where&lt;/a&gt; extension method, and then add it to our list and also fill in a dictionary of types.&lt;/p&gt;
&lt;p&gt;The bug with the above code is that the &lt;a href="http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx"&gt;foreach&lt;/a&gt; body will not be entered (regardless of the types contained in &lt;font face="Courier New" size="2"&gt;knownTypes&lt;/font&gt; or &lt;font face="Courier New" size="2"&gt;_knownTypes&lt;/font&gt;).&lt;/p&gt;
&lt;p&gt;In Linq, any queries have its &lt;a href="http://msdn.microsoft.com/en-us/library/bb943859.aspx"&gt;execution deferred&lt;/a&gt;; in the case above, the &lt;font face="Courier New" size="2"&gt;newTypes&lt;/font&gt; variable actually has the &lt;a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx"&gt;expression&lt;/a&gt; as to what needs to be done.  When calling &lt;a href="http://msdn.microsoft.com/en-us/library/z883w3dc(VS.80).aspx"&gt;AddRange&lt;/a&gt;, we pass in &lt;font face="Courier New" size="2"&gt;newTypes&lt;/font&gt; as the parameter, and the query is executed then and there.&lt;/p&gt;
&lt;p&gt;However, in the foreach, &lt;font face="Courier New" size="2"&gt;newTypes&lt;/font&gt; is still the same expression, so when it gets to that foreach line, the expression is re-evaluated again.  Guess what – at that point &lt;font face="Courier New" size="2"&gt;_knownTypes &lt;/font&gt;already contains all the types in &lt;font face="Courier New" size="2"&gt;newTypes&lt;/font&gt; , thus the query will return an empty set.&lt;/p&gt;
&lt;p&gt;We always think of using a variable to store a value and that value doesn’t change (unless we change it or some other code change it).  In this case, it’s actually like a delegate call, which will basically execute a method that will return the value.  I’ll be sure to remember this so I don’t get bit by this again.&lt;/p&gt;
&lt;p&gt;Edit:&lt;/p&gt;
&lt;p&gt;My friend &lt;a href="http://geekswithblogs.net/kgrossnicklaus/"&gt;Kevin Grossnicklaus&lt;/a&gt; aptly pointed out that Linq operators fall into 2 groups, the ones that have deferred execution and the ones that are not.  For instance &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.sum.aspx"&gt;Sum&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.count.aspx"&gt;Count&lt;/a&gt; and just about any of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx"&gt;Enumerable extension methods&lt;/a&gt; that return an actual concrete type (&lt;a href="http://msdn.microsoft.com/en-us/library/bb342261.aspx"&gt;ToList&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/bb298736.aspx"&gt;ToArray&lt;/a&gt; are 2 other examples) will actually execute the query then and there.&lt;/p&gt;
&lt;p&gt;It's when the method returns an IEnumerable&amp;lt;T&amp;gt; then the query gets deferred; which can lead to some frustrating bugs.  I'll illustrate some of these bugs in future post &lt;img alt="" src="/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135034"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=135034" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/135034.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/23/linq--deferred-execution--always-re-execute.aspx</guid>
            <pubDate>Wed, 23 Sep 2009 13:50:38 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/135034.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/23/linq--deferred-execution--always-re-execute.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/135034.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/135034.aspx</trackback:ping>
        </item>
        <item>
            <title>Methods parameters and return values, what types to use?</title>
            <link>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/18/methods-parameters-and-return-values-what-types-to-use.aspx</link>
            <description>&lt;p&gt;I’m trying to be a better developer, so I try to make sure I follow best practices as much as I can.  At times when I’m just creating simple methods though, every now and then I get somewhat hung up as to what do I use as types for my input parameters and return values.  You see, it is something that is so basic, we create methods all the time, but most of the time, we don’t pay much attention to it since we’d just like to continue on with the task at hand.  So I’m writing this to remind myself that writing methods, sometime is not as straightforward as it seems…&lt;/p&gt;  &lt;p&gt;A colleague was code-reviewing some project files and noticed that we have a same-intentioned method (same name, similar code on what it does but somewhat different input &amp;amp; return values) in 3 different classes.  Of course, the placement of the code itself is erroneous, but let’s attribute that to hectic development and developer’s unfamiliarity of existing classes / methods.  So, this colleague would like to have only 1 of these methods, essentially combining them.  This is what the method looks like:&lt;/p&gt;  &lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="600"&gt;         &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; GetMembers(&lt;span style="color: #2b91af"&gt;EntityCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; pieces)
{
   &lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;();

   &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Piece &lt;/span&gt;piece &lt;span style="color: blue"&gt;in &lt;/span&gt;pieces)
   {
      &lt;span style="color: blue"&gt;if &lt;/span&gt;(piece.IsMembers)
      {
         result.Add(piece);
      }
   }

   &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Now, the method itself is fairly simple; it accepts an &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;EntityCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;, (which is a collection type that we have created for our purposes) and then gets all the entities that passes the IsMembers check.  In the other 2 places, the method differs as follows:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetMembers(&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;[] pieceList)
{
    &lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();
    &lt;span style="color: green"&gt;// Same Code here...
&lt;/span&gt;}&lt;/pre&gt;

        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; GetMembers(&lt;span style="color: #2b91af"&gt;EntityCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; pieces)
{
   &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;();
   &lt;span style="color: green"&gt;// Same Code here...
&lt;/span&gt;}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;As you can see, the difference is in the return values and input parameter types.  The code inside is almost identical.  I can also relate with the developers that made these methods; when we create methods, we usually just take everything at face value.  In most cases when the method was written, the developer has a need for it and the collection that is used by that particular is an &lt;font style="color: #2b91af" face="Courier New" size="2"&gt;EntityCollection&lt;/font&gt;, thus easily enough, the method created accepts an &lt;font style="color: #2b91af" face="Courier New" size="2"&gt;EntityCollection &lt;/font&gt;as well.  All too often, this is how we write methods, in general, especially simple helper methods.  Similarly, it just so happens, the consuming code at the time just needed a &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms132397.aspx"&gt;Collection&lt;/a&gt;&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;, so the method was created returning that same type.&lt;/p&gt;

&lt;p&gt;Of course, looking at the above examples, this means the method is only useful if the particular input &amp;amp; output are that same exact type, thus from a junior developer’s perspective, it may seem to be fine to just create variants of this method; especially since .NET allows methods overloading.  Keep in mind that in my scenario, these methods are not even in the same class!  So the task now becomes, how can we collate all these into one nice method that can be reused?  Hopefully it won’t get changed much (if at all) afterwards?&lt;/p&gt;

&lt;p&gt;If you’re at all familiar with interfaces, after seeing these methods, the solution is very, very obvious.  The solution should be something like the following:&lt;/p&gt;

&lt;table cellspacing="1" cellpadding="1" width="600" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="600"&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; GetMembers(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; pieces)
{
   &lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt; result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;();

   &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Piece &lt;/span&gt;piece &lt;span style="color: blue"&gt;in &lt;/span&gt;pieces)
   {
      &lt;span style="color: blue"&gt;if &lt;/span&gt;(piece.IsMembers)
      {
         result.Add(piece);
      }
   }

   &lt;span style="color: blue"&gt;return &lt;/span&gt;result;
}&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;The input parameter is now of type &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; and the return value is of type &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt;.  It is very, very obvious; but how did we get here?  By looking at the disparate methods in existence, we (somewhat intuitively) know that by using interfaces, we can condense the various methods into one.  Careful readers would realize that it breaks consumers of the second variant, since the second variant returns a &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;Collection&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; but returning that as a type is bad practice anyway since the object we’re processing will always be &lt;span style="color: #2b91af"&gt;&lt;font face="Courier New" size="2"&gt;Piece&lt;/font&gt;&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;The question then would be, how can we get to create this method without first going through the types of consumers that is going to call it?  Over time, I created some generalization rules which in hindsight seem to work fairly well.  The rules for types in methods are as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Input parameters types should be of the most BASIC type that is needed by the code within the method; an interface whenever possible&lt;/li&gt;

  &lt;li&gt;Return values should be of the most ADVANCED type possible; an interface whenever possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea behind the 2 rules are fairly simple; in the example the method code only needs to go through each item in the passed in collection and as such &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt; fits perfectly.  The code would have worked with &lt;a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx"&gt;IList&amp;lt;T&amp;gt;&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx"&gt;ICollection&amp;lt;T&amp;gt;&lt;/a&gt;, but since no other methods are needed from those interfaces, between the 3 candidates we’ll choose the most BASIC type.  By using the most basic type, if a consumer happens to only have access to an &lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Piece&lt;/span&gt;&amp;gt;&lt;/font&gt;&lt;/font&gt; (maybe they get it from some LINQ manipulations) this method can still be called.&lt;/p&gt;

&lt;p&gt;For the return value, the reasoning is a bit more complex: the method can just return a type of Collection&amp;lt;Piece&amp;gt; and that can be made to work with any consuming code.  After all &lt;a href="http://msdn.microsoft.com/en-us/library/ms132397.aspx"&gt;Collection&amp;lt;T&amp;gt;&lt;/a&gt; implements &lt;a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx"&gt;IList&amp;lt;T&amp;gt;&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx"&gt;ICollection&amp;lt;T&amp;gt;&lt;/a&gt;, and &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt; and their non-generic variants.  However, if we choose to return an actual concrete type we may possibly have trouble in the future if we needed to return something else.  &lt;/p&gt;

&lt;p&gt;One recent example of changing return values that I had encountered was with the advent of WPF; WPF has great support for &lt;a href="http://msdn.microsoft.com/en-us/library/ms750612.aspx"&gt;data binding&lt;/a&gt; – but to make sure 2-way data binding works entities have to implement &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx"&gt;INotifyPropertyChanged&lt;/a&gt; and collections have to implement &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx"&gt;INotifyCollectionChanged&lt;/a&gt;.  So we ended up having to change most of our methods that returns a &lt;a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/a&gt; to return &lt;a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx"&gt;ObservableCollection&amp;lt;T&amp;gt;&lt;/a&gt;.  The methods that returned &lt;a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/a&gt;, we have to change the consumer code to store the return value as &lt;a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx"&gt;IList&amp;lt;T&amp;gt;&lt;/a&gt; to make it work.&lt;/p&gt;

&lt;p&gt;So that was one lesson that I took to heart, so I try to make public methods (and even private methods as much as possible) to have interfaces are turn values.  I hope that answers the question as to why I chose to return an interface versus returning an actual concrete class.  &lt;/p&gt;

&lt;p&gt;The secondary explanation that is required then is why did I chose to use an IList&amp;lt;T&amp;gt;, rather than an IEnumerable&amp;lt;T&amp;gt;?  With interfaces, I have the chose between &lt;a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx"&gt;IList&amp;lt;T&amp;gt;&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx"&gt;ICollection&amp;lt;T&amp;gt;&lt;/a&gt;, and &lt;a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/a&gt;.  The rule I have is that return values should be of the most ADVANCED type as possible, so I chose &lt;a href="http://msdn.microsoft.com/en-us/library/5y536ey6.aspx"&gt;IList&amp;lt;T&amp;gt;&lt;/a&gt;.  Why?  That’s because in the case we have here, the collection is generated and returned – it’s not being kept, it doesn’t affect any state and in essence the returned value can be used directly by the consuming code.  With that in mind, having the most advanced type returned allows consumers to immediately use the return value.  If the caller wanted to remove the last entry in the list, since we’re returning an IList&amp;lt;T&amp;gt;, the caller can do it using the returned value.  If we had chosen to return an IEnumerable&amp;lt;T&amp;gt;, then the caller have to create its own list and then do the removal process.  So the benefit is for the consuming code, since they can act on the return value better given the more advanced type being returned.&lt;/p&gt;

&lt;p&gt;Following my generalized rules, if the &lt;span style="color: #2b91af"&gt;&lt;font face="Courier New" size="2"&gt;Piece&lt;/font&gt;&lt;/span&gt; entity implements an interface (say IPiece), I would change the method to return IList&amp;lt;IPiece&amp;gt; and accepts IEnumerable&amp;lt;IPiece&amp;gt;.  However, this usually means most of your code should operate on the IPiece interface and rarely do so on the actual concrete &lt;span style="color: #2b91af"&gt;&lt;font face="Courier New" size="2"&gt;Piece&lt;/font&gt;&lt;/span&gt; class itself.&lt;/p&gt;

&lt;p&gt;Every now and then when I create methods, I go through this same thought process.  I hope to make this more seamless for me in the future, so I will automatically always generate methods that are easy to consume.  Hopefully this can be of use to other people.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134917"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=134917" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/NewThingsILearned/aggbug/134917.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Muljadi Budiman</dc:creator>
            <guid>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/18/methods-parameters-and-return-values-what-types-to-use.aspx</guid>
            <pubDate>Fri, 18 Sep 2009 19:03:34 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/NewThingsILearned/comments/134917.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/NewThingsILearned/archive/2009/09/18/methods-parameters-and-return-values-what-types-to-use.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/NewThingsILearned/comments/commentRss/134917.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/NewThingsILearned/services/trackbacks/134917.aspx</trackback:ping>
        </item>
    </channel>
</rss>