<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Dev Bros &#187; programming style</title>
	<atom:link href="http://www.webdevbros.net/category/programming-style/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdevbros.net</link>
	<description>hot talk about web development</description>
	<lastBuildDate>Thu, 20 Jan 2011 19:55:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Testing code changes, bugfixes, new features, …</title>
		<link>http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/</link>
		<comments>http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 23:00:52 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[general stuff]]></category>
		<category><![CDATA[programming style]]></category>

		<guid isPermaLink="false">http://fabiankoehler.de/wdb/?p=14</guid>
		<description><![CDATA[When you implement a new feature somewhere, when you change just some bits of your code, when you fix a bug, or you just change a common text in an app&#8230; What do you do afterwards? Do you really check the result or do you trust yourself that it works fine 100%. It is an [...]]]></description>
			<content:encoded><![CDATA[<p>When you implement a new feature somewhere, when you change just some bits of your code, when you fix a bug, or you just change a common text in an app&#8230; What do you do afterwards? Do you really check the result or do you trust yourself that it works fine 100%. It is an interesting thing to talk about…<span id="more-14"></span></p>
<p>I have to be honest and admit that I trusted myself a lot of times in the early days and at least 2 of 10 times I was wrong. If you deal with customers then it&#8217;s two times too much. Especially when you have that guy on the phone and you claim that you did change it, but he is browsing the application and does not see any changes. In that case you&#8217;re really pissed because you haven&#8217;t spent one minute to check the result/consequence of your change.</p>
<p>So that&#8217;s why I always suggest to check EVERYTHING that has been changed. You never know what you forget, but when you get sure that you see the change yourself then you&#8217;re on the safe side. Here is a list what you could not have thought about:</p>
<ul>
<li>you have been working in the wrong file. The file you have been working on was just a backup, in the wrong location, wrong server, etc. Such things happen everyday. </li>
<li>you forgot to refresh/restart some service, application, etc. </li>
<li>forgot to publish the file. This is common but i am sure it happens as well. For instance you forgot to upload the file on the server if its a web app. </li>
<li>forgot to compile. for all out there who need to do this. </li>
<li>last but not least programing failures which you haven&#8217;t thought about (especially when you are changing code of others you should 100% check the effect of the change even if it&#8217;s just a typo): you changed the wrong part of the code. Happens usually when there are duplicate pieces of code, which in turn happens because of bad programming style (think of the DRY principle).
</li>
</ul>
<p>Hence it&#8217;s highly recommended to check EVERY change. You save yourself and others a lot of troubles and iterations of changing and testing. Personally I think that this is a programmers quality characteristic and that it&#8217;s not a tall order to expect a check of the change. In most cases it&#8217;s less than a minute. Don&#8217;t get lazy …. For sure everybody does, but be aware of all the points I mentioned above. </p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/&amp;t=Testing+code+changes%2C+bugfixes%2C+new+features%2C+%E2%80%A6&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/&amp;title=Testing+code+changes%2C+bugfixes%2C+new+features%2C+%E2%80%A6&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/&amp;title=Testing+code+changes%2C+bugfixes%2C+new+features%2C+%E2%80%A6&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Testing+code+changes%2C+bugfixes%2C+new+features%2C+%E2%80%A6;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/08/20/testing-a-change-bugfix-implemented-feature-%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Service vs. Component</title>
		<link>http://www.webdevbros.net/2009/07/14/service-vs-component/</link>
		<comments>http://www.webdevbros.net/2009/07/14/service-vs-component/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 06:05:57 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[Pragmatic programmer]]></category>
		<category><![CDATA[programming style]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=374</guid>
		<description><![CDATA[I was having trouble to differentiate between both nor could I 100% explain to someone what&#8217;s the real difference between a Service and a Component. Today I&#8217;ve found a really nice definition by Martin Fowler which I would like to share with you: Component I use component to mean a glob of software that&#8217;s intended [...]]]></description>
			<content:encoded><![CDATA[<p>I was having trouble to differentiate between both nor could I 100% explain to someone what&#8217;s the real difference between a Service and a Component. Today I&#8217;ve found a really nice definition by <a href="http://www.martinfowler.com/">Martin Fowler</a> which I would like to share with you:</p>
<p><strong>Component</strong></p>
<blockquote><p>
I use component to mean a glob of software that&#8217;s intended to be used, without change, by application that is out of the control of the writers of the component. By &#8216;without change&#8217; I mean that the using application doesn&#8217;t change the source code of the components, although they may alter the component&#8217;s behavior by extending it in ways allowed by the component writers. &#8211; <a href="http://www.martinfowler.com/articles/injection.html">Martin Fowler, Inversion of Control Containers and the Dependency Injection pattern</a>
</p></blockquote>
<p><strong>Service</strong></p>
<blockquote><p>
A service is similar to a component in that it&#8217;s used by foreign applications. The main difference is that I expect a component to be used locally (think jar file, assembly, dll, or a source import). A service will be used remotely through some remote interface, either synchronous or asynchronous (eg web service, messaging system, RPC, or socket.) &#8211; <a href="http://www.martinfowler.com/articles/injection.html">Martin Fowler, Inversion of Control Containers and the Dependency Injection pattern</a>
</p></blockquote>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/07/14/service-vs-component/&amp;t=Service+vs.+Component&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/07/14/service-vs-component/&amp;title=Service+vs.+Component&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/07/14/service-vs-component/&amp;title=Service+vs.+Component&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Service+vs.+Component;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/07/14/service-vs-component/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/07/14/service-vs-component/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# tips, tricks and things you should know</title>
		<link>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/</link>
		<comments>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 11:33:25 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming style]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/</guid>
		<description><![CDATA[During my previous holidays (i have to admit 1.5 months surfing in portugal) I had some literature with me which also included the book Programming .net components released with O'Reilly. It talks about the aspects of components in general and provides good examples how to achieve those in .net. Although these pages are a very [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.webdevbros.net/wp-content/uploads/2007/11/dotnetcomponents.jpg' alt='.net components' align='left' /> During my previous holidays (i have to admit 1.5 months surfing in portugal) I had some literature with me which also included the book <a href="http://www.amazon.com/Programming-NET-Components-Juval-Lowy/dp/0596102070/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1195026384&#038;sr=8-1">Programming .net components</a> released with O'Reilly. It talks about the aspects of components in general and provides good examples how to achieve those in .net. Although these pages are a very good read (rather for experienced devs) for ppl interested in component development, I found it more interesting because of its general C# content. There are concepts &#038; approaches, small tips &#038; tricks and intersting things about C# which can be used not only in component based architectures and are therefore good to know anyway. </p>
<p>As I always make notes while reading "geek" books I thought: "so why not summarize them all up in a nice list and blog'em". Here we go...<span id="more-115"></span></p>
<p><strong>1. implicit vs explicit interface</strong><br />
implicit interface implementation allows to use the interface member through an object or through the interface whereas the explicit interface implementation allows only access through the interface.</p>
<div class="igBar"><span id="lcsharp-13"><a href="#" onclick="javascript:showCodeTxt('csharp-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-13">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//public interface IInterface {</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//implicit implementation</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> AClass : IInterface <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit implementation</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> BClass : IInterface <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">void</span> IInterface.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//usage implicit</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AClass a = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IInterface b = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">b.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//usage explicit</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IInterface c = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">c.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BClass d = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">d.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//Fails because of explicit implementation!!! </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Visual studio just offers the choice of the implementation but most people don't know the difference. With explicit implementation the server (implementing the interface) can prevent clients from using it directly on object which implements it. It forces that the members of the interfaces are only accessible via the interface itself</p>
<p><strong>2. implicit vs explicit cast</strong><br />
this is just to get used to the terminology because a lot of guys are using it but don't know the terminology ;)</p>
<div class="igBar"><span id="lcsharp-14"><a href="#" onclick="javascript:showCodeTxt('csharp-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-14">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//implicit cast</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> T</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit cast</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>y</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit cast (defensive)</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = y <span style="color: #0600FF;">as</span> T </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here you see that implicit casting is assigning a class instance directly to a variable. Explicit means casting from one type to another. </p>
<p><strong>3. Destructors</strong><br />
A destructor of a class is defined with ~ClassName. In comparison to Dispose() the C# destructor is always called. Dispose() needs to be called by the client whereas the destructor is called when the garbage collector comes around. Best way is to implement both and channel the implementation to the method which does the cleanup. e.g. cleanup. Example of implementation [COMP05, 98]:</p>
<div class="igBar"><span id="lcsharp-15"><a href="#" onclick="javascript:showCodeTxt('csharp-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-15">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyBase : IDisposable <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> _disposed = <span style="color: #0600FF;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">protected</span> <span style="color: #FF0000;">bool</span> Disposed <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; get <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> _disposed;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>!_disposed<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _disposed = <span style="color: #0600FF;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//the method for the subclasses if they need to release resources</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;~MyBase<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Classes deriving from this baseclass can now implement their destruction by overriding the cleanup method and can now decide to do the clean up manually (calling Dispose) or leave it till the GC comes around. Important here is that if Dispose() was called we tell the garbage collector to exclude this instance from being garbaged later (with GC.SuppressFinalize()).</p>
<p><strong>4. use "Using" defensive</strong></p>
<div class="igBar"><span id="lcsharp-16"><a href="#" onclick="javascript:showCodeTxt('csharp-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-16">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #0600FF;">as</span> IDisposable<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This approach will only call Dispose() if the object implements IDisposeable. Good if you dont really know if the object needs to release resources or not and if you want to be ready for future changes. e.g. the server decides to implement IDisposeable.</p>
<p><strong>5. Delegate inference</strong></p>
<div class="igBar"><span id="lcsharp-17"><a href="#" onclick="javascript:showCodeTxt('csharp-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-17">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//instead</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">publisher.<span style="color: #0000FF;">someDelegate</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#40;</span>someMethod<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//u can use</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">publisher.<span style="color: #0000FF;">someDelegate</span> += someMethod </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is much nicer to read but only supported with .net 2.0.</p>
<p><strong>6. Custom event arguments</strong><br />
Custom event arguments (own class derrived from EventArgs) should have only readonly members. This prevents the client from changing the values. Readonly members can only be assigned in the constructor so the publisher of the event is able to set the values when creating the arguments.</p>
<p><strong>7. Always fire events in a try catch block</strong><br />
You never know what the event handler method of the client does, so its good to put the firing into a try catch block. You could write your own EventsHelper class [COMP05, 144] for the purpose of firing event within your classes.</p>
<div class="igBar"><span id="lcsharp-18"><a href="#" onclick="javascript:showCodeTxt('csharp-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-18">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> EventsHelper <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Fire<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span> d, <span style="color: #0600FF;">params</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>d == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> dels = d.<span style="color: #0000FF;">GetInvocationList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span> sind <span style="color: #0600FF;">in</span> dels<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sink.<span style="color: #0000FF;">DynamicInvoke</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>8. use "property-like" approach for event accessors</strong><br />
As you do with common member variables (expressing them with properties) you also should do this with events. This alows you to control the adding and removing of event handlers for your event.</p>
<div class="igBar"><span id="lcsharp-19"><a href="#" onclick="javascript:showCodeTxt('csharp-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-19">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyClass <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;MyHandler _myEvent;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> MyHandler MyEvent <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; add <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_myEvent += value;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; remove <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_myEvent -= value;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>9. Static objects as keys</strong></p>
<div class="igBar"><span id="lcsharp-20"><a href="#" onclick="javascript:showCodeTxt('csharp-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-20">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">static</span> name = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myCollection<span style="color: #000000;">&#91;</span>name<span style="color: #000000;">&#93;</span> = <span style="color: #808080;">"a name"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//instead of (old school)</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myCollection<span style="color: #000000;">&#91;</span><span style="color: #808080;">"name"</span><span style="color: #000000;">&#93;</span> = <span style="color: #808080;">"a name"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
a static object is created to act as a key for e.g. collections (because every instance is unique - has its own identity!). The advantage in comparison to the "old school" version (key is a string) is that the key is already available in memory wheras the string needs to be created everytime. If you use this key in a lot of places it could save some time.</p>
<p><strong>10. Combine enumeration values</strong></p>
<div class="igBar"><span id="lcsharp-21"><a href="#" onclick="javascript:showCodeTxt('csharp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-21">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> color <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;red,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;green,</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;all = red | green</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This example illustrates that it is possible to have enumeration values which accept more than one existing values of the current enumeration.</p>
<p><strong>11. Singelton Application</strong><br />
A singelton application is one which only allows running one instance per machine. e.g. Microsoft Outlook is a singelton application. Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one. The source for an application which runs only once is available in another post "<a href="http://www.webdevbros.net/2007/11/14/singelton-application-with-c/">Singelton Application with C#</a>"</p>
<p><strong>12. Containment</strong><br />
If it is not possible to derive from a class we use a class to wrap around the class we would like to inherit from. This procedure is also known as "Containment" (I mention it here because the this term is more unknown - usually developers use wrapper)</p>
<p><strong>13. Use Debug.Assert()</strong><br />
The use of Debug.Assert should be used as often as possible. During the development the developer should "assert" things which always should be true. This helps finding bugs easier and its easier for other developers to understand the code.</p>
<div class="igBar"><span id="lcsharp-22"><a href="#" onclick="javascript:showCodeTxt('csharp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-22">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> a = <span style="color: #FF0000;color:#800000;">10</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> b = <span style="color: #FF0000;color:#800000;">20</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> c = <span style="color: #FF0000;color:#800000;">20</span> - <span style="color: #FF0000;color:#800000;">10</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Debug.<span style="color: #0000FF;">Assert</span><span style="color: #000000;">&#40;</span>c&gt; <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #808080;">"i need it bigger than 0. check might be done in the future"</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span> / c<span style="color: #000000;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This short example demonstrates that obviously the developer expects the variable "c" to be greater than 0. Why? Because in the next line he/she uses it for a dvision which would break with c = 0. If this is the case you would get an information seen in line 4. This concept is nice because it makes comments "active".</p>
<p><strong>14. user string instead of String</strong><br />
String is just an alias for string and therefore its nicer to use the original type straight away. The same with int and Int32, etc.</p>
<p><strong>15. Rethrowing exceptions</strong></p>
<div class="igBar"><span id="lcsharp-23"><a href="#" onclick="javascript:showCodeTxt('csharp-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-23">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//some logging,etc...</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">throw</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This piece of code rethrows the exception. The interesting thing here is that you dont need to specify the Exception explicitly. Just the keyword "throw" is enough to rethrow the exception. </p>
<p><strong>15. Code exclusion with attributes and not with #if .. #endif</strong></p>
<div class="igBar"><span id="lcsharp-24"><a href="#" onclick="javascript:showCodeTxt('csharp-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-24">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>Conditional<span style="color: #000000;">&#40;</span><span style="color: #808080;">"DEBUG"</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The advantage here is that the code is not reachable anymore if the condition is not met. This means that it will result in compile errors if you reference this method. #if ... #endif does not enforce this constraint. Its also nicer than the hash signs ;) Please check the <a href="http://msdn2.microsoft.com/en-us/library/aa664622(VS.71).aspx">MSDN Reference</a> for more details about the conditional attribute.</p>
<p>Thats was it :) Hope there was something interesting and maybe new for everyone. I dont want to promote the book here but I really recommend it because it contains good content about programming with c# (and especially for component development) and should be read by every C# developer who wants to develop for enterprise.</p>
<p>Maybe you have some interesting tips &#038; tricks which might be useful for others. Please be so nice and comment them... I am interested.</p>
<p>Resources:<br />
[COMP05] .net Components, Juval Löwy, O'Reilly 2005 United States</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;t=C%23+tips%2C+tricks+and+things+you+should+know&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;title=C%23+tips%2C+tricks+and+things+you+should+know&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;title=C%23+tips%2C+tricks+and+things+you+should+know&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=C%23+tips%2C+tricks+and+things+you+should+know;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

