<?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>Geekality &#187; C#</title>
	<atom:link href="http://www.geekality.net/tag/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +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>C#: How to send emails</title>
		<link>http://www.geekality.net/2010/05/16/c-how-to-send-emails/</link>
		<comments>http://www.geekality.net/2010/05/16/c-how-to-send-emails/#comments</comments>
		<pubDate>Sun, 16 May 2010 00:21:02 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=991</guid>
		<description><![CDATA[Sending a basic email message in a C# application is quite easy thanks to a class called SmptClient. We simply need an address to send to, an address to send from, the message we want to send and the client: &#8230; <a href="http://www.geekality.net/2010/05/16/c-how-to-send-emails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sending a basic email message in a C# application is quite easy thanks to a class called <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx">SmptClient</a>.</p>
<p>We simply need an address to send to, an address to send from, the message we want to send and the client:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;You&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That was pretty simple, wasn&#8217;t it? But what if we need to authenticate with our server? And what if we want to send our message in a more secure manner?</p>
<p><span id="more-991"></span></p>
<h2>Authentication</h2>
<p>You will quite often find that an <a href="http://en.wikipedia.org/wiki/Smtp_server">SMTP server</a> requires you to authenticate yourself before it will let you do anything. This is however very simple to do. All we need, is to provide our credentials to the client:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>Now, when you try to send your message like before, it should be sent without problems <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' />  But what if you want to send your email securely?</p>
<h2>Using SSL to encrypt the connection</h2>
<p>To send emails through an encrypted connection, all you have to do is to set the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx">EnableSSL</a> property of the SmtpClient to true:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; EnableSSL <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>When you now try to send your email, it will use the <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">Secure Socket Layer</a> to encrypt the connection. Like me, you might run into a problem though; an <a href="http://msdn.microsoft.com/en-us/library/system.security.authentication.authenticationexception.aspx">AuthenticationException</a> with the following message: <cite>The remote certificate is invalid according to the validation procedure.</cite></p>
<h2>Imperfect certificates</h2>
<p>After reading about <a href="http://www.verisign.com/ssl/ssl-information-center/how-ssl-security-works/">how these certificates work</a> and some more digging around I found out that the certificate of the SMTP server I use is considered invalid for two reasons: Firstly it was not issued by a trusted Certification Authority (like VeriSign or Thawte), but by my host (<a href="http://www.dreamhost.com/r.cgi?322827">Dreamhost</a>) themselves. And finally there is a mismatch between the domain I&#8217;m connecting to (smtp.example.com) and the domain the certificate was issued to (mail.dreamhost.com).</p>
<h2>Manual certificate validation</h2>
<p>SSL has two purposes: security and authentication. And the security will actually work fine and all the traffic will be encrypted even if the authentication fails. In other words, as long as we can look at the certificate ourselves and be sure that <em>we</em> thrust it, we can go ahead and continue with our work.</p>
<p>To check the certificate ourselves we can provide a <a href="http://msdn.microsoft.com/en-us/library/system.net.security.remotecertificatevalidationcallback.aspx">RemoteCertificateValidationCallback</a> delegate to a class called the <a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.aspx">ServicePointManager</a>.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span><br />
&nbsp; &nbsp; <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span></div></div>
<p>But how can we decide if we should trust the certificate or not? Well, I have found three ways that works nicely. Well, actually just two since the first way is to just cover your eyes and let anything pass.</p>
<h3>Pretend validation</h3>
<p>The following implementation is a way to simply say that <cite>I don&#8217;t care</cite>:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>This is of course not very good&#8230; we should at least make <em>some</em> effort and check if the certificate is the one we should be getting. We can for example&#8230;</p>
<h3>Check the certificate fingerprint</h3>
<p>Certificates have something called a fingerprint and if we know the fingerprint of the certificate we <em>should</em> get, then we can compare it with the fingerprint of the one we actually got.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCertHashString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;6B8C79AB966D70277BA86E6F820859A2B5B8CCC0&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// SHA-1 fingerprint</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>If you don&#8217;t think this is enough, you can for example&#8230;</p>
<h3>Check the whole certificate</h3>
<p>If you can export the certificate to a file somehow, you can then later compare the certificate you get with the one you have stored and make sure they are equal.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I got a hold of the certificate file by sticking the following snippet inside my validation delegate, ran my code once, and then changed it back to normal again:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var file <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var cert <span style="color: #008000;">=</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Export</span><span style="color: #008000;">&#40;</span>X509ContentType<span style="color: #008000;">.</span><span style="color: #0000FF;">Cert</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; file<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>cert, <span style="color: #FF0000;">0</span>, cert<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I also successfully exported it from Opera, which I use as an email client, by going to Preferences, Security, Manage Certificates, Approved and then export the one I want. This of course assumes you have have previously approved the certificate for use with Opera.</p>
<h2>Wrap-up</h2>
<p>That&#8217;s all for now! Found it kind of fun to get this working and thought I could share it in case someone else struggles with this. Please comment and let me know if I have misunderstood something or done a big blunder or something like that! I mostly just know that my code is working, so if you know more about how these security issues work and you know something you think i should know as well: Please share! I&#8217;d like to learn <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Here is my final complete code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Mail</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Authentication</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Cryptography.X509Certificates</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Security</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> Geekality<span style="color: #008000;">.</span><span style="color: #0000FF;">SecureEmail</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">class</span> Program<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.com.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Register our own certificate validation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;Myself&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Create client</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnableSsl <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Try to send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message sent!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>AuthenticationException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/05/16/c-how-to-send-emails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check for duplicates</title>
		<link>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/</link>
		<comments>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 16:57:09 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Extension methods]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=884</guid>
		<description><![CDATA[Say you have an IEnumerable&#60;T&#62; of some sort and you want to check if it contains any duplicates. How do you do that? Terrible solution I needed to do this a while ago and the first solution that hit me &#8230; <a href="http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Say you have an <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> of some sort and you want to check if it contains any duplicates. How do you do that?</p>
<p><span id="more-884"></span></p>
<h2>Terrible solution</h2>
<p>I needed to do this a while ago and the first solution that hit me wasn&#8217;t exactly good. Linq has method called Distinct, which returns the distinct items in a sequence (weeds out duplicates). Using that to check if duplicates exists you can do something like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var hasDuplicates <span style="color: #008000;">=</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">Distinct</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This is of course a <strong><em>really bad</em></strong> solution. So <em>don&#8217;t</em> use it! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<h2>Good solution</h2>
<p>The much better solution is to use a <code class="codecolorer text default"><span class="text">HashSet&lt;T&gt;</span></code>.</p>
<blockquote><p>The <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx">HashSet&lt;T&gt;</a> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.</p></blockquote>
<p>In addition to the fact that it cannot contain duplicate items, it also has a very nice add-method which returns false if the item is already in the set.</p>
<p>To check for duplicates using a hash set we can start out by assuming there is no duplicates and then add items to the hash set until we either run out of items, or we try to add an item that already exists. In the first case our assumption was right and in the second it turned out to be wrong.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var set <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var hasDuplicates <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var s <span style="color: #0600FF; font-weight: bold;">in</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>set<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hasDuplicates <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<h2>Good and handy solution</h2>
<p>Since we don&#8217;t want to type in that stuff over and over again we want this snippet in a nice extension method. Code snippet coming up <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> Geekality<span style="color: #008000;">.</span><span style="color: #0000FF;">Snippets</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> EnumerableExtensions<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> HasDuplicates<span style="color: #008000;">&#40;</span>subjects, EqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects, IEqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> comparer<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>subjects <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;subjects&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>comparer <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;comparer&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var set <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>comparer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var s <span style="color: #0600FF; font-weight: bold;">in</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>set<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Simple and handy. You use it for example like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">HasDuplicates</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Do something</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Hope it can help someone, somewhere, below and/or over the rainbow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Let me know what you think. I&#8217;m always open for improvements and feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to test asynchronous events</title>
		<link>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/</link>
		<comments>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 18:59:59 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Async]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=826</guid>
		<description><![CDATA[The other day I had to test that an event was raised after some asynchronous work had been done. And since I currently am a total test newbie, this was a new thing for me. Say we have this simple &#8230; <a href="http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The other day I had to test that an event was raised after some asynchronous work had been done. And since I currently am a total test newbie, this was a new thing for me. Say we have this simple shell of a class:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Worker<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler<span style="color: #008000;">&lt;</span>EventArgs<span style="color: #008000;">&gt;</span> Done<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">...</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Let&#8217;s just assume it does some work, and is supposed to raise the Done event when it is&#8230; well&#8230; done. <!-- AUSCVUHQEPXB --></p>
<p><span id="more-826"></span></p>
<h2>Synchronous worker</h2>
<p>If the Worker class works synchronously, it would be no problem at all to test it. For example:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">&#91;</span>TestFixture<span style="color: #008000;">&#93;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> WorkerTests<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start_WhenFinished_DoneEventIsRaised<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var worker <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; var eventWasRaised <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Done</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> eventWasRaised <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">That</span><span style="color: #008000;">&#40;</span>eventWasRaised<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>We simply add an event handler which sets a boolean flag when called, start the worker and then check if the flag was set or not. Easy peasy.</p>
<h2>Asynchronous worker</h2>
<p>What now if the Start method just sends the work to a new thread and returns immediately? Our current test can not be trusted at all in that case. It might pass and it might fail. Most likely fail. All depending on if it managed to finish its work and raise the event before we check the flag. And that would be unlikely to happen. Highly unlikely.</p>
<p>So what do we do? We use a <a href="http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx">ManualResetEvent</a> instead of the boolean flag:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">&#91;</span>TestFixture<span style="color: #008000;">&#93;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> WorkerTests<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Start_WhenFinished_DoneEventIsRaised<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var worker <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Worker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; var eventWasRaised <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManualResetEvent<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Done</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> eventWasRaised<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">That</span><span style="color: #008000;">&#40;</span>eventWasRaised<span style="color: #008000;">.</span><span style="color: #0000FF;">WaitOne</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">500</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Instead of checking that a flag has been set, we check that the event handler gives us a signal within a certain period of time. If it doesn&#8217;t give us that signal we can assume that the event was never raised or that the work simply took longer than we expected. Either case could probably be of interest <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Brilliant, simple and readable. Just how we like it <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
<h2>Credits</h2>
<p>I am still a total testing newbie at the moment and didn&#8217;t have a clue what to do, so after some failing google-fu I asked <a href="http://stackoverflow.com/questions/1770830/c-how-to-test-a-basic-threaded-worker-class">a question</a> about it at <a href="http://stackoverflow.com">StackOverflow</a>. My solution is based on <a href="http://stackoverflow.com/questions/1770830/c-how-to-test-a-basic-threaded-worker-class/1770862#1770862">one of the answers</a> I got there <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/26/how-to-test-asynchronous-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generics and checking for null</title>
		<link>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/</link>
		<comments>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 16:19:15 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Null]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=788</guid>
		<description><![CDATA[When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. Say we have this method which returns the subject if it is not null, and &#8230; <a href="http://www.geekality.net/2009/11/13/generics-and-checking-for-null/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When writing C#, in Visual Studio, using generics&#8230; have you ever tried checking for null? I have always found that a bit of a hassle. </p>
<p>Say we have this method which returns the subject if it is not null, and the result of a <code class="codecolorer text default"><span class="text">createNew()</span></code> function if it <em>is</em> null.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Not claiming this is a very useful method, but it was the simplest thing I could come up with <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Anyways, in Visual Studio you will now probably have a blue (is blue here at least) squiggly line under the <a href="http://msdn.microsoft.com/en-us/library/53k8ybth.aspx">equality operator</a>. It states you are doing a <cite>Possible compare of value type with &#8216;null&#8217;</cite>, which of course is reasonable and correct. We <em>could</em> just ignore it and move on&#8230; but we don&#8217;t really like squiggly lines, do we? I sure don&#8217;t&#8230; so lets get rid of it.</p>
<p><span id="more-788"></span></p>
<h2>Faulty solution</h2>
<p>ReSharper suggests that I use <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a> instead of null, like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">default</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Now, this won&#8217;t do at all. First of all, we don&#8217;t really want to check for <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx">default(T)</a>, we want to check for null. And second of all, we now have a long red squiggly line under the whole equality statement! It says that it <cite>Cannot apply operator &#8216;==&#8217; to operands of type &#8216;T&#8217; and &#8216;T&#8217;</cite>. That messages doesn&#8217;t really make much sense to me, to be honest, but I trust that it is true. Either way, we have to get rid of it.</p>
<h2>Good solution</h2>
<p><a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals(VS.80).aspx">ReferenceEquals</a> to the rescue! We can use that method, instead of the equality operator, and compare with null like we intended! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ReferenceEquals<span style="color: #008000;">&#40;</span>subject, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That is kind of ugly though&#8230; And it is <em>so</em> much longer to type. <em>And</em>, most importantly, it kind of makes the code a bit unreadable&#8230; At least if you have a bunch of things you have to check for null, like I did today.</p>
<p>But fear not! Today I came up with a tiny, but brilliant extension method which makes things a lot better:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> T subject<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> ReferenceEquals<span style="color: #008000;">&#40;</span>subject, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T NewIfNull<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T subject, Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> createNew<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>subject<span style="color: #008000;">.</span><span style="color: #0000FF;">IsNull</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> createNew<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> subject<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Doesn&#8217;t that just look <em>a lot</em> cleaner? I think so <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> </p>
<p>And yes, I know this isn&#8217;t really a big issue. And I realize you have probably all come up with something similar already. Yes, I am slow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> But hey, I figured it might be worth sharing this anyways, just in case you hadn&#8217;t thought about it before. I sure hadn&#8217;t! And I really do think that tiny snippet of code made a huge difference in code readability. And we all want that, don&#8217;t we? Yes, yes we do&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/13/generics-and-checking-for-null/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 25</title>
		<link>http://www.geekality.net/2009/11/06/project-euler-problem-25/</link>
		<comments>http://www.geekality.net/2009/11/06/project-euler-problem-25/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 22:51:56 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Big Int]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fibonacci]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=751</guid>
		<description><![CDATA[The Fibonacci sequence is defined by the recurrence relation: , where and . Hence the first 12 terms will be: &#8230; The 12th term, , is the first term to contain three digits. What is the first term in the &#8230; <a href="http://www.geekality.net/2009/11/06/project-euler-problem-25/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p>The Fibonacci sequence is defined by the recurrence relation:<br />
<img src="http://quicklatex.com/cache/ql_0fdba176bee77c46041880def6869c26.gif" alt="F_n = F_{n-1} + F_{n-2}" title="F_n = F_{n-1} + F_{n-2}" style="vertical-align: -4px; border: none;"/>, where <img src="http://quicklatex.com/cache/ql_9abbbedef650f272f894f1980cfc726d.gif" alt="F_1 = 1" title="F_1 = 1" style="vertical-align: -4px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_c25d7581be770023ed580dd28a11e2ff.gif" alt="F_2 = 1" title="F_2 = 1" style="vertical-align: -3px; border: none;"/>.</p>
<p>Hence the first 12 terms will be:</p>
<ul>
<li><img src="http://quicklatex.com/cache/ql_a286990398936a23b6a69c6dc468f5f3.gif" alt="F_1=1" title="F_1=1" style="vertical-align: -4px; border: none;"/></li>
<li><img src="http://quicklatex.com/cache/ql_43740a8477bf58183df88835dae8b0c3.gif" alt="F_2=1" title="F_2=1" style="vertical-align: -3px; border: none;"/></li>
<li>&#8230;</li>
<li><img src="http://quicklatex.com/cache/ql_fb13cc1a61dde7cb48f20e8330b12e46.gif" alt="F_{11}=89" title="F_{11}=89" style="vertical-align: -4px; border: none;"/></li>
<li><img src="http://quicklatex.com/cache/ql_84019eaa270ede73f5285cc8dbb74e98.gif" alt="F_{12}=144" title="F_{12}=144" style="vertical-align: -4px; border: none;"/></li>
</ul>
<p>The 12th term, <img src="http://quicklatex.com/cache/ql_ada838adeba1adb6961337c649fb8d67.gif" alt="F_{12}" title="F_{12}" style="vertical-align: -4px; border: none;"/>, is the first term to contain three digits.</p>
<p>What is the first term in the Fibonacci sequence to contain 1000 digits?</p></blockquote>
<p><span id="more-751"></span></p>
<h2>Solution</h2>
<p>We touched on the Fibonacci sequence a while ago in the <a href="/?p=165">second euler problem</a>. This time, however, we are in a whole different league. That time we were supposed to sum up all even Fibonacci numbers below 4 million, which might sound like a lot, but not when we compare it to the number we are asked for this time! The last Fibonacci number below 4 million is 3524578. That would be 7 digits. The maximum value of the datatype I used in the generator I made for that (ulong) is 18446744073709551615. That would be 20 digits. Quite a distance left to a thousand!</p>
<p>So, once again we turn to our big integer class, <a href="http://intx.codeplex.com/">IntX</a>. And since we have that, a solution to this problem is actually pretty straight forward. We simply have to make a new generator that uses the IntX data type instead of tiny ulong (it&#8217;s all relative&#8230;).</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> LargeFibonacciSequence <span style="color: #008000;">:</span> IEnumerable<span style="color: #008000;">&lt;</span>IntX<span style="color: #008000;">&gt;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>IntX<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var a <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var b <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntX<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> a<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var c <span style="color: #008000;">=</span> a <span style="color: #008000;">+</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a <span style="color: #008000;">=</span> b<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; IEnumerator IEnumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>The straight forward solution should then be fairly obvious.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var n <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
var sequence <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LargeFibonacciSequence<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var e <span style="color: #008000;">=</span> sequence<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnumerator</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveNext</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; n<span style="color: #008000;">++;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
var answer <span style="color: #008000;">=</span> n<span style="color: #008000;">;</span></div></div>
<p>That pretty much just moves through one Fibonacci number after the other, increasing <code class="codecolorer text default"><span class="text">n</span></code> as it goes, until it reaches the first one that has 1000 or more digits.</p>
<p>Very brute-force, not very fast (takes around 500 ms) and not very interesting, but that&#8217;s pretty much it&#8230; ooor is it?</p>
<h2>Take two!</h2>
<p>Alright, while reading up on Fibonacci numbers I found some interesting mathematical formulas and such. I&#8217;m not going to come with an in-depth explanation or lots proofs here, but I will share the formulas that matters and go through how we use them. If you want the elaborate explanations you can find them where smarter people than I reign. For example at <a href="http://en.wikipedia.org/wiki/Fibonacci_series">Wikipedia</a>, or at <a href="http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html">this site</a>. Anyways, here we go. Take cover if you fear math&#8230;</p>
<h3>The Fibonacci sequence</h3>
<p>The <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>th number in the Fibonacci sequence, <img src="http://quicklatex.com/cache/ql_f67871cd00ac973d0e2b80db93f3bcd3.gif" alt="F_n" title="F_n" style="vertical-align: -3px; border: none;"/>, is defined by the recurrence relation</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_0fdba176bee77c46041880def6869c26.gif" alt="F_n = F_{n-1} + F_{n-2}" title="F_n = F_{n-1} + F_{n-2}" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>with the seed values <img src="http://quicklatex.com/cache/ql_0aeae6a4e96c1dbcf45b521e505c7569.gif" alt="F_0=0" title="F_0=0" style="vertical-align: -3px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_9abbbedef650f272f894f1980cfc726d.gif" alt="F_1 = 1" title="F_1 = 1" style="vertical-align: -4px; border: none;"/>.</p>
<p>This recurrence, since it is linear, can be expressed by a <a href="http://en.wikipedia.org/wiki/Closed-form_expression">closed-form solution</a> known as the Binet&#8217;s formula:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_0ee0f9cdc2a0daeb61a218d0edf60f26.gif" alt="Fib(n)=\dfrac{\varphi^n-(1-\varphi)^n}{\sqrt{5}}=\dfrac{\varphi^n-(\frac{-1}{\varphi})^n}{\sqrt{5}}" title="Fib(n)=\dfrac{\varphi^n-(1-\varphi)^n}{\sqrt{5}}=\dfrac{\varphi^n-(\frac{-1}{\varphi})^n}{\sqrt{5}}" style="vertical-align: -17px; border: none;"/><br />
<img src="http://quicklatex.com/cache/ql_68252e18a2b40c61e0c66506189f3cc7.gif" alt="\varphi=\dfrac{1+\sqrt{5}}{2}\approx1.6180339887\ldots" title="\varphi=\dfrac{1+\sqrt{5}}{2}\approx1.6180339887\ldots" style="vertical-align: -12px; border: none;"/> &#8212; (The Golden ratio)</p></blockquote>
<p>Now, why <img src="http://quicklatex.com/cache/ql_c9dae022cefb2682530b2aa3885538b9.gif" alt="1-\varphi" title="1-\varphi" style="vertical-align: -4px; border: none;"/> and <img src="http://quicklatex.com/cache/ql_b828f2117f6246a0460a91c0cde036e7.gif" alt="\frac{-1}{\varphi}" title="\frac{-1}{\varphi}" style="vertical-align: -9px; border: none;"/> is considered the same, or even why that formula works or looks like that, I will leave as an exercise for the reader (Cause I have no idea <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Please share if you do&#8230;). But anyways, as an example, if you put 20 into that formula, you will get 6765 which is the 20th number in the Fibonacci sequence. Fantastic. We could now use that formula to make another brute-force solution to find the term we are after. But that wouldn&#8217;t make much sense and would be much much slower. So, we must move on further into this crazy land.</p>
<p>Before we move on to the next part, since we will be working with quite large values of <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, we can simplify that expression a bit. That is because <img src="http://quicklatex.com/cache/ql_69e8045f0432b985de5f2b55c21140e8.gif" alt="\frac{-1}{\varphi}^n" title="\frac{-1}{\varphi}^n" style="vertical-align: -9px; border: none;"/> will move pretty fast towards 0 as <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/> increases. In other words, we can pretty much just skip that part and use the following formula instead:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_7c09cd950e24a31d6319adfd2e1693e5.gif" alt="Fib(n)=\dfrac{\varphi^n}{\sqrt{5}}" title="Fib(n)=\dfrac{\varphi^n}{\sqrt{5}}" style="vertical-align: -17px; border: none;"/></p></blockquote>
<h3>The length of a number</h3>
<p>How do you calculate the length of a number? I actually had to do this in an earlier post and then I kind of cheated. I just converted the number into a string and just count the characters (In C#, <code class="codecolorer text default"><span class="text">string.Length</span></code>). But, I recently discovered that there actually is a way you can do this mathematically! I had no idea&#8230; The key lays in the <a href="http://en.wikipedia.org/wiki/Log10">logarithm to base 10</a>.</p>
<p><img src="http://quicklatex.com/cache/ql_5e4f1016aaa4ce9a1b18798cd4ab8774.gif" alt="\log_{10}{n}" title="\log_{10}{n}" style="vertical-align: -5px; border: none;"/> (often written as just <img src="http://quicklatex.com/cache/ql_b2fd4a0c24f17cef1f3cf4a6f14573ad.gif" alt="\log{n}" title="\log{n}" style="vertical-align: -3px; border: none;"/>) for any 1-digit number will give you 0.something. For any 2-digit number it gives you 1.something, and so on. So a formula for getting the length of any number:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_d35af591fd67f30ad1bdbcfa89361096.gif" alt="Len(n)=\lfloor\log{n}\rfloor+1" title="Len(n)=\lfloor\log{n}\rfloor+1" style="vertical-align: -5px; border: none;"/></p></blockquote>
<p>In case you were wondering, those weird square brackets are called the <a href="http://en.wikipedia.org/wiki/Floor_function">floor function</a>.</p>
<h3>The length of a Fibonacci number</h3>
<p>Now we are able to calculate the length of a certain Fibonacci number by using the following formula, which I for no good reason will call G:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_afd0ad738732831b1a6c4cd618cd916b.gif" alt="G(n)=Len(Fib(n))=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" title="G(n)=Len(Fib(n))=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" style="vertical-align: -17px; border: none;"/></p></blockquote>
<p>There is a problem here though. And the problem is <img src="http://quicklatex.com/cache/ql_793ba3f348c170a1bd0ad338c7abea40.gif" alt="\varphi^n" title="\varphi^n" style="vertical-align: -4px; border: none;"/>. Why is it a problem? Because it get&#8217;s <em>seriously</em> large very quick. A value of around 500 actually makes my calculator overflow and just give me an error.</p>
<p>But fear not! There are a couple of formulas having to do with roots and logarithms that we can use to our advantage:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_a6d99d0e5fc3214b85bf8ff764bf0eae.gif" alt="\sqrt{c}=\sqrt[2]{c}" title="\sqrt{c}=\sqrt[2]{c}" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_bdd731176b3acd420b9f3b5a6be51028.gif" alt="\sqrt[b]{c}=c^{\frac{1}{b}}" title="\sqrt[b]{c}=c^{\frac{1}{b}}" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_fa72a9e784a5a9b5522448e6a34511c4.gif" alt="\log_a{\dfrac{b}{c}}=\log_a{b}-\log_a{c}" title="\log_a{\dfrac{b}{c}}=\log_a{b}-\log_a{c}" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_1b1dafabfbc4d3a1d23aae1456b5b5f8.gif" alt="\log_a{b^c}=c\log_a{b}" title="\log_a{b^c}=c\log_a{b}" style="vertical-align: -4px; border: none;"/></p></blockquote>
<p>With those formulas we can handily rewrite our formula into one that is easier to handle:</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_fd38d11ece4c20c897ce531c6f9579e4.gif" alt="G(n)=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" title="G(n)=\lfloor\log{\dfrac{\varphi^n}{\sqrt{5}}}\rfloor+1" style="vertical-align: -17px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_06ca9e3545b294e3724dd36b01c011bb.gif" alt="G(n)=\lfloor\log{\varphi^n}-\log{5^{\frac{1}{2}}}\rfloor+1" title="G(n)=\lfloor\log{\varphi^n}-\log{5^{\frac{1}{2}}}\rfloor+1" style="vertical-align: -5px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_3162d75a6efd53bb980fbd3b95df4316.gif" alt="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" title="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" style="vertical-align: -12px; border: none;"/></p></blockquote>
<p>Tadaa! Now we can push in 20 and get 4. And as we calculated earlier, the 20th Fibonacci number is 6769, which in fact is 4 digits long! Amazing&#8230;</p>
<h3>The first Fibonacci number with 1000 digits</h3>
<p>Now we are almost ready to solve our problem. The formula we have now works perfectly. The only problem is that it&#8217;s backwards! We are not looking for the length of a certain Fibonacci number, but rather what Fibonacci number has a certain length. In other words, we want <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, not <img src="http://quicklatex.com/cache/ql_a417655e890e7887367fc17c4fd83bbf.gif" alt="G(n)" title="G(n)" style="vertical-align: -4px; border: none;"/>. Luckily, using pretty basic algebra and some guessing, we can make a new formula that finds exactly what we are looking for. (My way of skipping the floor stuff and introducing a ceiling function in the end is purely based on guessing and observation. But it gives the correct answer <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Now, if you know the proper way of handling it, please let me know! )</p>
<blockquote><p><img src="http://quicklatex.com/cache/ql_3162d75a6efd53bb980fbd3b95df4316.gif" alt="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" title="G(n)=\lfloor n\log{\varphi}-\dfrac{\log{5}}{2}\rfloor+1" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_40db6dd9aa223bd01d050d018a23b95d.gif" alt="n\log{\varphi}=G(n)+\dfrac{\log{5}}{2}-1" title="n\log{\varphi}=G(n)+\dfrac{\log{5}}{2}-1" style="vertical-align: -12px; border: none;"/></p>
<p><img src="http://quicklatex.com/cache/ql_ded0cb679459c3d86d75cd647dd9224c.gif" alt="n=\lceil\dfrac{G(n)+\dfrac{\log{5}}{2}-1}{\log\varphi}\rceil" title="n=\lceil\dfrac{G(n)+\dfrac{\log{5}}{2}-1}{\log\varphi}\rceil" style="vertical-align: -16px; border: none;"/></p></blockquote>
<p>Now we can just substitute <img src="http://quicklatex.com/cache/ql_a417655e890e7887367fc17c4fd83bbf.gif" alt="G(n)" title="G(n)" style="vertical-align: -4px; border: none;"/> with 1000, and calculate <img src="http://quicklatex.com/cache/ql_7b8b965ad4bca0e41ab51de7b31363a1.gif" alt="n" title="n" style="vertical-align: 0px; border: none;"/>, which should be the answer to this problem!</p>
<p>As usual, I will not put the actual answer here though <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> </p>
<p>This has been the longest blog post yet about these problems, I think, but I must say it was pretty fun to solve this one. I didn&#8217;t really get much at first, just saw that the formulas worked, but now that I have written about it and gone through it all step by step, it actually makes sense. Most of it anyways. If you know how to properly handle that floor and ceiling stuff, let me know <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p>Anyways, that was it for now! Stay tuned for more good stuff like this <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/06/project-euler-problem-25/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler: Problem 16</title>
		<link>http://www.geekality.net/2009/11/05/project-euler-problem-16/</link>
		<comments>http://www.geekality.net/2009/11/05/project-euler-problem-16/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 16:46:16 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Big Int]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=740</guid>
		<description><![CDATA[and the sum of its digits is. What is the sum of the digits of the number ? Solution With the big integer type IntX (which I added to my project to solve an earlier problem) this one is pretty &#8230; <a href="http://www.geekality.net/2009/11/05/project-euler-problem-16/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote><p><img src="http://quicklatex.com/cache/ql_1c50c7f8d9c3e1a0c8a1c256d4adfd7e.gif" alt="2^{15} = 32768" title="2^{15} = 32768" style="vertical-align: 0px; border: none;"/> and the sum of its digits is<br/><img src="http://quicklatex.com/cache/ql_668af5ed992c9e14c23ce75b24c3f209.gif" alt="3 + 2 + 7 + 6 + 8 = 26" title="3 + 2 + 7 + 6 + 8 = 26" style="vertical-align: -1px; border: none;"/>.</p>
<p>What is the sum of the digits of the number <img src="http://quicklatex.com/cache/ql_8c307f59bbe61715d115649a7bc3282c.gif" alt="2^{1000}" title="2^{1000}" style="vertical-align: 0px; border: none;"/>?</p></blockquote>
<p><span id="more-740"></span></p>
<h2>Solution</h2>
<p>With the big integer type <a href="http://intx.codeplex.com/">IntX</a> (which I added to my project to solve an earlier problem) this one is pretty much as easy as you&#8217;d think.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var answer <span style="color: #008000;">=</span> IntX<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">-</span> <span style="color: #666666;">'0'</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>It first does the calculation and then gets the string representation of the result. And in case you were wondering, that would be:</p>
<blockquote><p>10715086071862673209484250490600018105614048117<br />
05533607443750388370351051124936122493198378815<br />
69585812759467291755314682518714528569231404359<br />
84577574698574803934567774824230985421074605062<br />
37114187795418215304647498358194126739876755916<br />
55439460770629145711964776865421676604298316526<br />
24386837205668069376</p></blockquote>
<p>And as a comparison, here is the largest number that can be represented using built-in .net types. <code class="codecolorer text default"><span class="text">decimal.MaxValue</span></code>.</p>
<blockquote><p>79228162514264337593543950335</p></blockquote>
<p>As you can see, the first number is kind of bigger than the second <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> Aaaanyways, the next bit might be a bit cryptic. But fear not, it is actually quite simple:</p>
<ul>
<li>A string is an array of characters.</li>
<li>All characters have a <a href="http://en.wikipedia.org/wiki/Character_code">character code</a> (a numeric value).</li>
<li>The character codes for the numeric characters (&#8217;0&#8242;, &#8217;1&#8242;, and so on) comes after each other, which means that for example &#8217;1&#8242; has a character code value which is 1 larger than the character code of &#8217;0&#8242;.</li>
<li>If we take a character code and subtracts it from itself, we get zero. <cite>(Thank you captain obvious&#8230;)</cite></li>
<li>This means that if we subtract &#8217;0&#8242; from &#8217;0&#8242;, we get 0. And if we subtract &#8217;0&#8242; from &#8217;5&#8242;, we get 5. <cite>(Oh, right, clever!)</cite></li>
</ul>
<p>So, the last two lines in my code simply converts all the characters in the string into actual numbers and then sums them together. Which would get us the answer! Tadaa <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='^_^' title='^_^' class='wp-smiley smiley-9' /> </p>
<p>How did you do? Have you found a more interesting solution? Please do share <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/05/project-euler-problem-16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Art of Unit Testing</title>
		<link>http://www.geekality.net/2009/11/03/the-art-of-unit-testing/</link>
		<comments>http://www.geekality.net/2009/11/03/the-art-of-unit-testing/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 21:04:42 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit-Testing]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=722</guid>
		<description><![CDATA[Have you gone through three years of computer science bachelor degree fun (or anything similar) and pretty much not heard a word about testing? Or have you heard from all your teachers that testing is extremely important, but never learned &#8230; <a href="http://www.geekality.net/2009/11/03/the-art-of-unit-testing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.geekality.net/wp-content/uploads/2009/11/osherove_cover150.jpg" alt="The Art of Unit Testing (Book cover)" title="The Art of Unit Testing (Book cover)" width="150" height="186" class="alignright size-full wp-image-723" />Have you gone through three years of computer science bachelor degree fun (or anything similar) and pretty much not heard a word about testing? Or have you heard from all your teachers that testing is <em>extremely important</em>, but never learned how to even write one? That has been the case for me. <cite>Testing is important, you all got to do it, it is very important, always test, do it a lot!</cite> Well, sure&#8230; but how do I do it? How do I write one of these tests?</p>
<p><span id="more-722"></span></p>
<p>The last book I finished is called <a href="http://www.manning.com/osherove/">The Art of Unit Testing</a>, and I am <em>so</em> happy that I stumbled over that book. Spring 2009 I was at a developer conference called <a href="http://www.ndc2009.no/">NDC 2009</a> (Norwegian Developer Conference) and it was <em>really</em> great. If you have the time, you should go to that website and have a look. You can watch most of the talks there online. Anyways, the second day I attended a series of talks about <a href="http://en.wikipedia.org/wiki/Test_driven_development">Test Driven Development</a> with a great speaker called <a href="http://weblogs.asp.net/ROsherove/">Roy Osherove</a>. I learned a bunch and decided that TDD looks like something I should try to learn. However, I had a big problem. I didn&#8217;t even know how to write a test. And doing TDD without knowing how to write a test is kind of difficult&#8230; to say the least&#8230;</p>
<p>Anyways, this Roy Osherove person advertised that he had just finished a book about pretty much exactly that; writing unit tests. So, I decided to get myself a copy of it. </p>
<p>It was great! Seriously learned <em>a lot</em>. It covers the following subjects (yes, that would be the table of contents).</p>
<ol>
<li>The basics of unit testing</li>
<li>A first unit test</li>
<li>Using stubs to break dependencies</li>
<li>Interaction testing using mock objects</li>
<li>Isolation (mock object) frameworks</li>
<li>Test hierarchies and organization</li>
<li>The pillars of good tests</li>
<li>Integrating unit testing into the organization</li>
<li>Design and testability (Appendix A)</li>
<li>Extra tools and frameworks (Appendix B)</li>
</ol>
<p>It is only 283 pages long (including the appendixes) and a pretty quick and easy read. Which I like. I got sucked in, and it was finished <em>fast</em>. Probably should read it again some time <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> But anyways: I previously did not even know how to write a single unit test in any language. Now I do <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>You can get it from for example <a href="http://www.manning.com/osherove/">Manning</a> (who is the publisher) or from <a href="http://amzn.com/1933988274">Amazon</a>. I would probably get it from Amazon since they seem to have a 34% discount currently&#8230; Except I already have it, of course <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p>If you want to learn how to do unit testing, I can recommend it with all my heart <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Oh, by the way, when you buy this book, you even get a free eBook version of it! Which is totally brilliant, since then you can have the book and read it at home, but you can also for example put the eBook version on your <a href="http://en.wikipedia.org/wiki/Netbook">netbook</a> and read while travelling. Awesome all the way <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> Wish more publishers or authors (or whoever decides) were like that&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/11/03/the-art-of-unit-testing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
