Tag: .NET

Tech Quiz: Some micro optimization

Optimize the following code: doubletotalCount = 0; for (int index = 0; index < numberOfRuns; index++) {     string[] values = GetData(index);     totalCount += values.Count(); } Answer A simple optimization is to replace Count() with Length. Count() is an extension method that looks something like this: public static int Count<TSource>(this IEnumerable<TSource> source){    if (source == null)    { ...

TechQuiz Question 6: Getting some closure…

What will be the output of the following code? using System; using System.Collections.Generic; class Test {     delegate void Printer();         static void Main()     {         List<Printer> printers = new List<Printer>();         for (int i = 0; i < 10; i++)         {             printers.Add(delegate { Console.WriteLine(i); });         }                 foreach (Printer printer in printers)        ...

Unit Testing, hell or heaven?

This blog is a translation of my article that’s published by the Dutch Microsoft .NET Magazine (http://www.dotnetmag.nl/Artikel/2683/Unit-testen,-hemel-of-hel) Unit Testing is a practice of which we are all convinced that it’s a good thing. I’ve seen many teams that start out very enthusiastic with Unit Testing, but in practice it’s often disappointing. Tests are time consuming to maintain, slow running and...