teranex weblog - .nethttps://budts.be/2010-12-18T12:31:00+01:00Implicit conversions in C#2009-03-25T18:51:00+01:002010-12-18T12:31:00+01:00Jeroen Budtstag:budts.be,2009-03-25:/weblog/2009/03/implicit-conversions-in-c<p>Yesterday I came across an interesting article: <a href="http://journal.stuffwithstuff.com/2008/04/08/whats-the-opposite-of-nullable/">What's the opposite of Nullable</a>. While the solution for Non-Nullability is interesting, the reason i'm blogging this is because the article also used a C# feature which i didn't know of: implicit conversions.</p>
<p>And guess what? Today I had a situation where I could use these implicit conversions. My app reads data from a CSV-file, so all the input are just strings. Until now that was just fine. However, at one part of my code I had to process one of the fields which has a fixed format. Say a field is a phonenumber and i need the country-prefix. So I created a PhoneNumber-class like this:</p>
<div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">class</span> <span class="nc">PhoneNumber</span>
<span class="p">{</span>
<span class="k">private</span> <span class="k">readonly</span> <span class="kt">string</span> <span class="n">_number</span><span class="p">;</span>
<span class="k">public</span> <span class="nf">PhoneNumber</span><span class="p">(</span><span class="kt">string</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(!</span><span class="k">new</span> <span class="n">Regex</span><span class="p">(</span><span class="n">PHONE_NUMBER_REGEX</span><span class="p">).</span><span class="n">IsMatch</span><span class="p">(</span><span class="n">number</span><span class="p">))</span>
<span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentException</span><span class="p">(</span><span class="s">"Invalid phone number"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">_number</span> <span class="p">=</span> <span class="n">number</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Prefix</span>
<span class="p">{</span>
<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">GetPrefixFromNumber</span><span class="p">(...);</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Now it is possible to create a (helper-)method in another class to get the prefix like this (this is just a simple example):</p>
<div class="highlight"><pre><span></span><span class="k">public …</span></pre></div><p>Yesterday I came across an interesting article: <a href="http://journal.stuffwithstuff.com/2008/04/08/whats-the-opposite-of-nullable/">What's the opposite of Nullable</a>. While the solution for Non-Nullability is interesting, the reason i'm blogging this is because the article also used a C# feature which i didn't know of: implicit conversions.</p>
<p>And guess what? Today I had a situation where I could use these implicit conversions. My app reads data from a CSV-file, so all the input are just strings. Until now that was just fine. However, at one part of my code I had to process one of the fields which has a fixed format. Say a field is a phonenumber and i need the country-prefix. So I created a PhoneNumber-class like this:</p>
<div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">class</span> <span class="nc">PhoneNumber</span>
<span class="p">{</span>
<span class="k">private</span> <span class="k">readonly</span> <span class="kt">string</span> <span class="n">_number</span><span class="p">;</span>
<span class="k">public</span> <span class="nf">PhoneNumber</span><span class="p">(</span><span class="kt">string</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(!</span><span class="k">new</span> <span class="n">Regex</span><span class="p">(</span><span class="n">PHONE_NUMBER_REGEX</span><span class="p">).</span><span class="n">IsMatch</span><span class="p">(</span><span class="n">number</span><span class="p">))</span>
<span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentException</span><span class="p">(</span><span class="s">"Invalid phone number"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">_number</span> <span class="p">=</span> <span class="n">number</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Prefix</span>
<span class="p">{</span>
<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">GetPrefixFromNumber</span><span class="p">(...);</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Now it is possible to create a (helper-)method in another class to get the prefix like this (this is just a simple example):</p>
<div class="highlight"><pre><span></span><span class="k">public</span> <span class="kt">string</span> <span class="nf">GetPrefix</span><span class="p">(</span><span class="n">PhoneNumber</span> <span class="n">phoneNumber</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">phoneNumber</span><span class="p">.</span><span class="n">Prefix</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>But when you only have the phonenumber as a string, you'll still have to create an instance of PhoneNumber to be able to call the GetPrefix method:</p>
<div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">prefix</span> <span class="p">=</span> <span class="n">GetPrefix</span><span class="p">(</span><span class="k">new</span> <span class="n">PhoneNumber</span><span class="p">(</span><span class="s">"+32485123456"</span><span class="p">));</span>
</pre></div>
<p>That is, until we add a method for implicit conversion to the PhoneNumber-class:</p>
<div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">class</span> <span class="nc">PhoneNumber</span>
<span class="p">{</span>
<span class="k">private</span> <span class="kt">string</span> <span class="n">_number</span><span class="p">;</span>
<span class="k">public</span> <span class="nf">PhoneNumber</span><span class="p">(</span><span class="kt">string</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(!</span><span class="k">new</span> <span class="n">Regex</span><span class="p">(</span><span class="n">PHONE_NUMBER_REGEX</span><span class="p">).</span><span class="n">IsMatch</span><span class="p">(</span><span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentException</span><span class="p">(</span><span class="s">"Invalid phone number"</span><span class="p">,</span> <span class="s">"number"</span><span class="p">);</span>
<span class="p">}</span>
<span class="n">_number</span> <span class="p">=</span> <span class="n">number</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">public</span> <span class="kt">string</span> <span class="n">Prefix</span>
<span class="p">{</span>
<span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">GetPrefixFromNumber</span><span class="p">(...);</span> <span class="p">}</span>
<span class="p">}</span>
<span class="k">public</span> <span class="k">static</span> <span class="k">implicit</span> <span class="k">operator</span> <span class="nf">PhoneNumber</span><span class="p">(</span><span class="kt">string</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nf">PhoneNumber</span><span class="p">(</span><span class="n">number</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>Now it is possible to call the GetPrefix method with just a string. The string will be automagically converted to a PhoneNumber, unless it is invalid, in which case the ArgumentException will be thrown:</p>
<div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">prefix</span> <span class="p">=</span> <span class="n">GetPrefix</span><span class="p">(</span><span class="s">"+32485123456"</span><span class="p">);</span>
</pre></div>Techdays2009-03-12T23:30:00+01:002010-12-18T12:31:00+01:00Jeroen Budtstag:budts.be,2009-03-12:/weblog/2009/03/techdays<p>Gisteren mocht ik voor het werk een dagje van de <a href="http://www.microsoft.com/belux/techdays/">Microsoft Techdays</a> bijwonen. Het was de eerste keer dat ik naar een Microsoft-event ging en had me eerlijk gezegd aan veel show en weinig inhoud verwacht. In sterk contrast tot <del>JavaPolis</del> <a href="http://www.javoxx.com/">Devoxx</a>, waar er veel inhoud en weinig show is. Achteraf bekeken viel dit eigenlijk enorm goed mee. Enkel de keynote ging er wat over. Een Developer Evangelist of Regional Director moet echt niet als één of andere superster worden aangekondigd met loeiharde muziek en lichtshow, maar dat is mijn mening natuurlijk.</p>
<p>Na de keynote heb ik de sessie over C# 4.0 gevolgd en dat was erg interessant. C# 4.0 belooft weer een versie te worden om naar uit te kijken. Eén van de nieuwe features is het <code>dynamic</code> keyword. Dit zorgt ervoor dat je methodes op het object kan oproepen waarbij de compiler niet checked of deze effectief bestaan. De check wordt pas 'at runtime' gedaan. Dit is erg handig om te integreren met andere talen zoals …</p><p>Gisteren mocht ik voor het werk een dagje van de <a href="http://www.microsoft.com/belux/techdays/">Microsoft Techdays</a> bijwonen. Het was de eerste keer dat ik naar een Microsoft-event ging en had me eerlijk gezegd aan veel show en weinig inhoud verwacht. In sterk contrast tot <del>JavaPolis</del> <a href="http://www.javoxx.com/">Devoxx</a>, waar er veel inhoud en weinig show is. Achteraf bekeken viel dit eigenlijk enorm goed mee. Enkel de keynote ging er wat over. Een Developer Evangelist of Regional Director moet echt niet als één of andere superster worden aangekondigd met loeiharde muziek en lichtshow, maar dat is mijn mening natuurlijk.</p>
<p>Na de keynote heb ik de sessie over C# 4.0 gevolgd en dat was erg interessant. C# 4.0 belooft weer een versie te worden om naar uit te kijken. Eén van de nieuwe features is het <code>dynamic</code> keyword. Dit zorgt ervoor dat je methodes op het object kan oproepen waarbij de compiler niet checked of deze effectief bestaan. De check wordt pas 'at runtime' gedaan. Dit is erg handig om te integreren met andere talen zoals Javascript en Ruby.</p>
<p>Een andere interessante feature is 'Generic co- and contra- variance'. Hiermee kan je in speciale gevallen casts uitvoeren zoals:</p>
<div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">list</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p"><</span><span class="kt">string</span><span class="p">>();</span>
<span class="p">(</span><span class="n">List</span><span class="p"><</span><span class="kt">object</span><span class="p">>)</span><span class="n">list</span><span class="p">;</span>
</pre></div>
<p>Ook optional en named parameters behoren tot de nieuwe features. Dit is ook erg interessant. Bekijk dit voorbeeldje:</p>
<div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">void</span> <span class="nf">Order</span><span class="p">(</span><span class="kt">string</span> <span class="n">name</span><span class="p">,</span> <span class="kt">int</span> <span class="n">amount</span> <span class="p">=</span> <span class="m">2</span><span class="p">,</span> <span class="kt">int</span> <span class="n">discount</span> <span class="p">=</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="n">Order</span><span class="p">(</span><span class="s">"jeroen"</span><span class="p">,</span> <span class="n">dicount</span><span class="p">:</span> <span class="m">5</span><span class="p">);</span>
<span class="c1">// the first param - name - is not optional so a value must be supplied.</span>
<span class="c1">// the second param - amount - has default value of 2 which we want to use</span>
<span class="c1">// the third param - discount - has a default value of 0 but we supply 5</span>
</pre></div>
<p>Enkele belangrijke punten bij het gebruikt van deze feature zijn wel dat je parameters absoluut als constante moet beschouwen. Je kan na verloop van tijd in veel gevallen immers niet zomaar de standaard waarde van een parameter aanpassen. Als je in voorgaand voorbeeld de default value van amount zou wijzigen in 3 zouden er plots veel klanten 3 items bestellen in plaats van 2. Ook is het wijzigen van de naam van een parameter niet meer zo vanzelfsprekend omdat deze naam nu ook buiten de methode wordt gebruikt.</p>
<p>De volgende sessie die ik heb bijgewoond ging over WCF. Hoewel de inhoud nogal vrij droog was, was ook dit wel interessant. Vooral de spreker was nogal een grapjas.</p>
<p>De daaropvolgende presentatie ging over WPF en meer specifiek over performantie. Aan de hand van een demo-applicatie waarin een reeks foto's werden ingeladen werden er enkele gangbare performantie problemen getoond en hoe veel van deze problemen vaak eenvoudig opgelost kunnen worden. Bij het laden van 50 foto's gebruikte de applicatie in het begin 1.3 GB geheugen. Na enkele eenvoudige aanpassingen nog slechts 220 MB. Het belangrijkste deel van de oplossing was om ervoor te zorgen dat het aantal elementen in de WPF-tree zo laag mogelijk werd gehouden. Dit werd onder andere bereikt door een VirtualStackPanel te gebruiken ipv een gewone StackPanel, zodat enkel elementen die effectief op het scherm getoond worden ook berekend worden. Een interessante tool die je hierbij kan helpen is <a href="http://blois.us/Snoop/">Snoop</a>. Dit is een open source programma dat ongeveer hetzelfde werkt als de DOM-explorer in Firefox.</p>
<p>Nog een andere sessie die ik heb gevolgd ging over Lean Principles, Agile Techniques, and Team System. Hierbij weerlegde de presentator 7 gangbare foute veronderstellingen ivm software ontwikkeling. Ook liet hij zien hoe je dit kan toepassen mbhv TFS. Gelukkig ging het niet al teveel over TFS en meer over de algemene theorie waardoor ook deze sessie erg leerrijk was.</p>
<p>De laatste sessie ging over Windows 7 for developers. Tijdens de keynote was Windows 7 al even gedemonstreerd, met nadruk op de (multi-)touch features. Tijdens deze presentatie lag de nadruk echter op de gevolgen en impact van Windows 7 voor developers. Natuurlijk werd er wel eerst een globaal overzicht getoond van de nieuwe taskbar en desktop. Die nieuwe taskbar gaat voor vele mensen even wennen zijn, maar ik denk wel dat het potentieel heeft. Een van de andere nieuwe features die me zo nog te binnen schieten die wel interessant zijn, is het beheer van services. In windows 7 is het namelijk mogelijk om te specifiëren wanneer een bepaalde service moet draaien. Een service die een netwerk verbinding nodig heeft verbruikt bijvoorbeeld enkel maar resources als er geen netwerkverbinding is. In Windows 7 kan die service adhv service triggers op de gepaste momenten in en uitgeschakeld worden.</p>
<p>Ook knap was de problem solver. In Windows XP zat er al een soort van problem solver ingebouwd, maar dit was niet erg interessant. Het stelde oplossingen voor die de gebruiker manueel kon proberen en vroeg telkens of het probleem was opgelost om dan een andere oplossing voor te stellen indien het probleem nog steeds aanwezig was. In Windows 7 gebeurt dit geheel automatisch aan de hand van PowerShell-scripts (als ik het correct begrepen heb). Programma's kunnen ook hun eigen problem solver scripts installeren, zodat dit systeem volledig uitbreidbaar is. Voor veel voorkomende problemen lijkt me dit echt wel een interessante en gebruiksvriendelijk feature. Of deze feature echt zal werken hangt natuurlijk af van de hoeveelheid problemen die de problem solver effectief kan oplossen. De demo was knap, maar het was natuurlijk nog altijd een voorbereid 'probleem' (de audio service werd gestopt en de problem solver wist deze correct terug te starten).</p>
<p>Ik heb echter nog wel een bedenking die ik me achteraf nog heb gemaakt na het bekijken van de presentatie over Windows 7. Windows 7 bevat standaard functionaliteit om je eigen applicatie van een ribbon te voorzien (een soort van knoppenbalk zoals in Office 2007), en er werd sterk benadrukt naar developers toe om dit ook te gebruiken. Tijdens andere presentaties kon je al een preview zien van Visual Studio 2010. Het vlaggenschip voor development van Microsoft. En wat valt mij daarbij op? Microsoft maakt hier zelf geen gebruik van de ribbon. Mij lijkt dit toch weer een inconsistentie in de Windows-omgeving. Of zou Microsoft dan toch niet zoveel vertrouwen hebben in de ribbon?</p>ASP.NET MVC 1.0 Quickly2009-02-18T18:06:00+01:002010-12-18T12:31:00+01:00Jeroen Budtstag:budts.be,2009-02-18:/weblog/2009/02/aspnet-mvc-10-quickly<p>Today I was pretty surprised when going through my feeds to read that a friend of mine, Maarten Balliauw, <a href="http://blog.maartenballiauw.be/post/2009/02/17/Announcing-my-book-ASPNET-MVC-10-Quickly.aspx">has a written a book</a>. The title of the book is <a href="https://www.packtpub.com/asp-net-model-view-controller-1-0-quickly/book">ASP.NET MVC 1.0 Quickly</a>:</p>
<blockquote>It’s been quite a job, but there it is: Packt just announced my very first book on their site. It is titled “ASP.NET MVC 1.0 Quickly”, covering all aspects ASP.NET MVC offers in a to-the-point manner with hands-on examples. The book walks through the main concepts of the MVC framework to help existing ASP.NET developers to move on to a higher level. It includes clear instructions and lots of code examples. It takes a simple approach, thereby allowing you to work with all facets of web application development. Some keywords: Model-view-controller, ASP.NET MVC architecture and components, unit testing, mocking, AJAX using MS Ajax and jQuery, reference application and resources.</blockquote>
<p>I always thought, and still think, that writing a book is a massive amount of work. Therefore i really …</p><p>Today I was pretty surprised when going through my feeds to read that a friend of mine, Maarten Balliauw, <a href="http://blog.maartenballiauw.be/post/2009/02/17/Announcing-my-book-ASPNET-MVC-10-Quickly.aspx">has a written a book</a>. The title of the book is <a href="https://www.packtpub.com/asp-net-model-view-controller-1-0-quickly/book">ASP.NET MVC 1.0 Quickly</a>:</p>
<blockquote>It’s been quite a job, but there it is: Packt just announced my very first book on their site. It is titled “ASP.NET MVC 1.0 Quickly”, covering all aspects ASP.NET MVC offers in a to-the-point manner with hands-on examples. The book walks through the main concepts of the MVC framework to help existing ASP.NET developers to move on to a higher level. It includes clear instructions and lots of code examples. It takes a simple approach, thereby allowing you to work with all facets of web application development. Some keywords: Model-view-controller, ASP.NET MVC architecture and components, unit testing, mocking, AJAX using MS Ajax and jQuery, reference application and resources.</blockquote>
<p>I always thought, and still think, that writing a book is a massive amount of work. Therefore i really have a lot of respect for people that have the courage to actually write a real book.</p>
<p>In his blogpost Maarten explains why and how he wrote the book. According to his time tracking software, it took him 100 hours to write the book, which is far less than i would expect. On the other side, if you can only use spare time to do the work, it is a lot of time. Maarten also explains that he used Subversion to keep his work versioned and that some people laughed at him for doing so. I don't think this is such a strange idea. If I ever would write a book (consider the chances small...), I certainly would use Subversion, or a similar tool, to store my work. In fact, when my girlfriend wrote her paper for her last schoolyear I also made her use Subversion to store her word-documents. But i do find it strange that you write a complete book in Word (which is a word processor btw, not a text editor ;) ).</p>
<p>I'm very curious to see and read the book. Btw Maarten, is that your own pair of glasses on the cover? Anyway, Congrats Maarten!</p>