Mark Michaelis' Weblog :
Updated: 9/1/2004; 7:04:12 AM.

 








Subscribe to "Mark Michaelis' Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Subscribe To
Mark's Weblog

 
 

Thursday, September 11, 2003

C# foreach with arrays
Google Search It

It is commonly shown that a foreach loop compiles into the CIL (Common Intermediate Language) equivalent of the enmerator pattern.  In other words, the code snippet shown here:

ArrayList array = new ArrayList();

... 

foreach (int i in array)

{

     Console.WriteLine(i);

}

Will end compile into the CIL equivalent of this:

ArrayList array;

int number;

IEnumerator enumerator;

IDisposable disposable;

 

array = new ArrayList();

...

enumerator = array.GetEnumerator();

try

{

     while (enumerator.MoveNext())

     {

          number = ((int)enumerator.Current);

          Console.WriteLine(number);

     }

}

finally

{

     disposable = (enumerator as IDisposable);

     if (disposable != null)

     {

          disposable.Dispose();

     }

}

 What is not often shown, however, is that if a C# array is used in place of the collection class (ArrayList in the above code) like this snippet here:

int[] array =

array = new int[]{1, 2, 3, 4, 5, 6};

 

foreach (int i in array)

{

     Console.WriteLine(i);

}

Then the C# compiler will create the CIL equivalent of the following:

int number;

int[] tempArray;

int[] array = new int[]{1, 2, 3, 4, 5, 6};

 

tempArray = array;

for (int counter = 0; (counter < tempArray.Length); counter++)

{

 Console.WriteLine(tempArray[counter]);

}

 In other words, the C# compiler optimizes the foreach loop based on the data that is being iterated over.  One can imagine that another optimization will be made once generics are available such that the enumerator pattern shown above will avoid the extra cast assuming that the collection supports IEnumerator<T> :

IEnumerator<int> enumerator = ...

...

while(enumerator.MoveNext())

{

     number = /* NO CAST */ enumerator.Current;

     Console.WriteLine(number);

}

...


1:01:18 PM   []    comment []

My Team and eXtreme/Agile Programming
Google Search It
One of the many roles I play at work is as the architect for a framework team that writes components using C# and .NET.   About ten months ago we adopted an eXtreme Programming/Agile process.  This essentially means we were not militaristic about XP and instead used the parts of it that made sense to us and morphed the other parts.  I recommend that teams considering XP should adopt it in its entirety at first for at least a couple months and then customize afterward.  Otherwise, you wont be able to truly evaluate what is worth while and what isn't.  Many of the items we have relaxed on I have since regretted and I find it much more difficult to go back to these practices once they were discarded.  Furthermore, some of the practices I initially hated, story cards for example, I now appreciate.  I still hate writing on cards but being able to shuffle these around during a planning meeting is wonderful.  Test driven development seems to be the item that demands the most discipline but I can also say unequivocally that this also offers the greatest benefit.  We have abandoned pair programming, probably the most controversial of the practices, and I confess I am sorry about this too.  I still use pair programming on non-work related projects except for code maintenance or minor tweaks.

8:02:33 AM   []    comment []

eXtreme Programming Book Recommendations
Google Search It

I was recently asked by a co-worker out of our Raleigh office about eXtreme Programming book recommendations.  I haven't read that much but what I have read I wholeheartedly recommend.  The two book I read are:

Test Driven Development: By Example
Author: Beck
Format: Paperback
Publication Date: November 2002
ISBN: 0321146530
List Price: $ 29.99
 
and
 
Extreme Programming Installed
Author: Ron Jeffries,Ann Anderson,Chet Hendrickson
Format: Paperback
Publication Date: September 2000
ISBN: 0201708426
List Price: $ 29.95
 
The first one is broken in to three sections.  I really only read the theory section at thought it provided the information I was seeking.  A co-worker read the first section which was a practical programming example and he really liked that part.  Either way, this is a great way to understand test driven development I recommend it.  The second book is a quick read and gives you the XP fundamentals.

7:52:13 AM   []    comment []

© Copyright 2004 Mark Michaelis.



 


September 2003
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Aug   Oct


Recent Posts