I can’t use var in my foreach loop

A month ago, I asked a coworker to implement IEnumerable<blah> on a blahCollection type that we had implemented back in 1.1. We had only recently moved to the C# 3.0 compiler for this project and I was a little surprised that I wasn’t able to use the var keyword inside a foreach statement of this type. (I could use it, but it types the object as object instead of a strong type, which is effectively “cannot use it”.)

Its a combination of “we used to implement our own iterators” and the way the foreach finds the GetEnumerator method. foreach doesn’t actually work off of IEnumerable or IEnumerable<T>. It just looks for a GetEnumerator method. So when you implement IEnumerable<T>, foreach won’t find it if it is an explicit implementation. It needs to be an implicit implementation. But this will conflict with the old IEnumerable implementation, and so the change is a break in binary compatibility. Its a small price to pay for use of new language features.

note: Bill Wagner told us all of the above but wrapped it in a <guess> tag when he emailed us.

I wrote a test to show the behavior. Huge thanks to someone (I don’t recall who) in #csharp on freenode who helped me in creating the CompileTimeType method. Its very different to me to use the compiler in this way.

using System;
using System.Collections.Generic;
using System.Collections;
using NUnit.Framework;
namespace Scratch.IEnumerableFixtures
    public class foreachFixture
        public void implicitIsStrong()
            foreach(var item in new I())
                Assert.AreEqual(typeof(int), CompileTimeType(item));
        public void explicitIsWeak()
            foreach (var item in new E())
                Assert.AreEqual(typeof(object), CompileTimeType(item));
        public void noInterface()
            foreach (var item in new N())
                Assert.AreEqual(typeof(object), CompileTimeType(item));
        public void noInterfaceGeneric()
            foreach (var item in new NG())
                Assert.AreEqual(typeof(int), CompileTimeType(item));
        public Type CompileTimeType<T>(T item) { return typeof(T); }
        class I : IEnumerable<int>, IEnumerable
            public IEnumerator<int> GetEnumerator() { yield return 1; }
            IEnumerator IEnumerable.GetEnumerator() { yield return 1; }
        class E : IEnumerable<int>, IEnumerable
            IEnumerator<int> IEnumerable<int>.GetEnumerator() { yield return
1; }
            public IEnumerator GetEnumerator() { yield return 1; }
        class N { public IEnumerator GetEnumerator() { yield return 1; } }
        class NG { public IEnumerator<int> GetEnumerator() { yield return
1; } }

VS2k8 and VS2k5 so close… so far away…

Expanding on some thoughts which I put in reply to http://igloocoder.com/archive/2007/12/12/opening-vs2k5-.net-2.0-projects-in-vs2k8.aspx 

The issue isn’t that Visual Studio 2008 (VS2k8) has “sweet compiler sugar that allows you to use the new language features in .NET 2.0, 3.0 or 3.5”.

VS2k8 is ALWAYS using the C# 3.0 compiler. PERIOD. done.* C# 3.0 code ALWAYS compiles down to IL which will run on the 2.0 runtime (there is no newer runtime).

So the “2.0, 3.0, 3.5” combo box is really just a filter for what assemblies you reference. Have you referenced WPF? Then studio (msbuild) will warn you that this is not a 2.0 target, and to use 3.0.  Trying to use LINQ or something else from System.Core? You will get a warning to use 3.5.

I’ve done this, and I was (like the igloo coder) a little surprised at what I saw. Then I went and clicked “3.0” and things worked. But it was just an assembly reference that did it. It is nice if you have to deploy to Windows 2000 servers or workstations which only have 2.0 runtime and cannot move to 3.0, or if you target XP machines which can’t install 3.0 or 3.5. But it isn’t the ultimate in 2005 to 2008 migration for which some of us were hoping.

What is a bummer is that all the nice languages features such as auto properties and use of ‘var’ will not ever work in VS2k5. So even though the project format is portable, its a bit useless. This bit me too. I had left a project set at 2.0, and I just started coding in 2008. That damn ‘var’ implicit typing just saves on keystrokes so much that I use the heck out of it. The next day I openned up the 2k5 solution, which openned the same project and now my project didn’t compile. OOPS. Luckily I hadn’t defined any new types using the nice property syntax. I would have cried given all the extra typing I had to do. My fingers hurt from this rant.

* Ok, so ASP.NET can be told which compiler to use, but arguably that isn’t VS doing the compile then, it is ASP.NET doing the compile. Maybe there is an MSBuild target which I can specify which will let me use the old 2.0 compiler so that my use of ext methods, var and property syntax is caught?

February AADND meeting on Wednesday the 14th.

Jennifer Marsman and Drew Robbins, Developer Evangelists for
Microsoft’s Heartland District, will be speaking at the Ann Arbor .NET
Developers Group
on Windows Presentation Foundation with talks entitled “3D
Graphics in the Windows Presentation Foundation” and “Layouts,
Styles and Templates in Windows Presentation Foundation”. They decided
that they would team-up for the meeting with Jennifer covering our tutorial
session and Drew the main. If you are looking for a jumpstart on WPF, don’t
miss AADND on February 14th starting at 6:00 pm at the Ann Arbor IT
Zone Spark Central located at 330 E. Liberty, Ann Arbor, MI 48104.


Tutorial with Jennifer Marsman: 3D Graphics in the Windows Presentation


The Windows Presentation Foundation is Microsoft’s
unified presentation platform for Windows.  WPF provides an integrated set
of APIs allowing the developer to create 2D and 3D content with ease. 
Developers and designers can construct scalable, high-quality content in both
two and three dimensions using WPF’s advanced vector-based rendering
system.  In this session, we will discuss how to create 3D graphics in WPF.


Main Session with Drew Robbins: Layouts, Styles and
Templates in Windows Presentation Foundation


Windows Presentation Foundation is Microsoft’s unified
presentation subsystem for Windows, which enables developers and designers to
create visually stunning user experiences. One powerful feature of Windows
Presentation Foundation is the separation of the appearance of controls and the
behavior of controls. In this session, we’ll look at the power of layouts,
styles and templates and how you’ll use them in your applications. We’ll also
look at the underlying concepts that make them work and how you can use them to
compose your own WPF components.

Mono is here to stay.

I couldn’t agree less with the idea that Mono is dead. That is the nice way that I say that I think someone is so dumb that they should not be allowed to write and communicate their opinions with others. I’m talking to you Neil McAllister. Mono is here to stay. I refuse to type the title or subtitle of the article to which I am referring.

Open sourcing Java under the GPL has no benefits compared to the state of Mono. At least half of the points regarding Microsoft and patents in the article are addressed on the Mono website under the Licensing FAQ. Neil McAllister is worse than the slime of the Open Source community that is Bruce Parens. Bruce just makes very stupid comparisons of closed source to the abomination of slavery. Neil just flat out lies. Alright, I retract that. Neil is not worse, he is equally low slime.

Neil seems to think that Java is just as good as the CLR (the runtime which runs .NET and Mono applications.

…recent releases of Java have done much to close the gap. Why go over to the dark side when established, mature technology is already available?

I’ll quickly point out that words like ‘dark side’ should immediately trigger the readers FUD detector. This type of illogic is no different than the FUD that Microsoft marketing once propagated which said that closed source was less than secure and written by uneducated teenagers.

What is worse is to realize that Neil believes that Java has done much to close the gap. He obviously has not spoken to anyone who has used both technologies. With Mono and .NET I have the choice of using C#, VB.NET (I can still use the ms compiler and run my code on mono), Iron Python, Boo, F#, and countless other languages. With Java I can choose to program in Java, or… well maybe Groove. Sorry, JPython doesn’t work with modern JVM. Now consider all of the features of all of those languages and compare. Java has TONS of catching up to do.

On this point I’d like to comment that I hope Java catches up and blasts past .NET. It will only mean that Microsoft will be forced to do the same. A healthy game of competitive leapfrog could be very great for developers using both technologies.

Another licensing issue which I would like to pull directly from the Mono licensing faq is just why Mono is NOT licensed GPL. Mono uses an interesting mix of licensing. The compiler and tools are licensed under the GPL. Runtime libraries are licensed under the LGPL. The class libraries are licensed under MIT X11. This means that I don’t have to worry about the GPL unless I am modifying the compiler. I can write software, even closed source, and I can link to the LGPL Mono runtime. I can ship my software closed source, along with an open source mono runtime. With Java under the GPL, technically any software linking to the java libraries must also be GPL. I am not a lawyer but does this means it is illegal to use the GPL version of Java with closed source software? It sounds like it.

The Mono class libraries are licensed under the MIT X11 license for exactly this reason. From the Mono Licensing FAQ:

Originally, the class libraries were released under the terms of the GNU Library GPL (LGPL). The problem with the GNU LGPL is an outdated wording related to “derived works”. A derived work of the library must be covered by the same license as the library itself. This definition was fine before object oriented frameworks existed, but with the introduction of object oriented frameworks, different people disagree whether some code that uses object-oriented inheritance is an instance of a “derived work”.

This is completely ignoring that a high number of the best Linux desktop applications are written in C# and run under Mono. F-spot is the iPhoto application for Linux. Tomboy is sticky notes merged with a desktop wiki. Banshee is quickly becoming an alternative to iTunes for syncing audio with an ipod and does things like transcoding from lossless codecs that iTunes does not do.

On the commercial front, Mainsoft actually uses Mono and IKVM to sell an application that helps people take their ASP.NET applications and run them on a J2EE webserver. At this point the line between Mono and Java is getting blurred. IKVM is an awesome bridge. All those existing Java libraries are accessible from Mono and visa-versa.

It is a shame that ComputerWorld would publish such an uninformed piece, but sensationalism sells.

Dumping stored procedures using boo.

A while ago David Cumps posted about Extracting stored procedure content via SQL.

I liked this post and I knew I wanted to do something with it.  We want to keep a copy of all of our stored procedures in Subversion, our version control system.  This little boo script connects a database server, iterates through all of the stored procedures in all of the databases and outputs them to a directory named after the database.

import System
import System.Data from "System.Data"
import System.Data.SqlClient from "System.Data"
import System.IO

def Main(argv as (string)):

    Exit = System.Environment.Exit
    mkdir = System.IO.Directory.CreateDirectory

    if(null!=argv and argv.Length>0):
        databasehost = argv[0]
        databasehost = "MyDefaultDbServer"

    ignoreDatabases= ["master", 'model', 'msdb', 'Northwind', 'pubs', 'tempdb']

    connectionString = string.Format("server={0};database=master;Integrated Security=True",databasehost)
    print "using connection string:"+connectionString
    conn = SqlConnection(connectionString)
    cmd = conn.CreateCommand()
    cmd.CommandText="SELECT name FROM master.dbo.sysdatabases ORDER BY name"
    reader = cmd.ExecuteReader()
    while reader.Read() :
        if ( not ignoreDatabases.Contains( reader.GetString(0) )):

    print databases

    for database in databases:
        cmd = conn.CreateCommand()
        cmd.CommandText = "use ${database};SELECT name FROM sysobjects WHERE type = 'P' AND category = 0 ORDER BY name"
        reader = cmd.ExecuteReader()
        while reader.Read() :

        print "${database}: ${spNames}"
        for spName in spNames:
            cmd = conn.CreateCommand()
            cmd.CommandText = "use ${database}; SELECT text FROM syscomments WHERE id=(SELECT id FROM sysobjects WHERE name='${spName}') ORDER BY colid"
            reader = cmd.ExecuteReader()
            outputf = File.CreateText( Path.Combine(database, "${spName}.sql") )
            while reader.Read():

powered by performancing firefox

Maybe we like Firefox because it is less fragile

Maybe we like Firefox because it cannot be screwed up as easily as Internet Explorer.


Somehow, ALL new IE windows would fail for me.  At first I thought the popup blocker was screwy, but then (weeks or months later) I realized that right clicking and picking open in a new window was also not working.  A quick use of google gave me the above link.

I don’t see how this same kind of thing could happen with Firefox.  Maybe Firefox will be the better browser for my Mom because it can’t as easily get hindered like IE did in this case.

I know that I’ll never have to register COM dlls to fix a Firefox bug.  My fix for IE was easy enough:
regsvr32 shdocvw.dll
regsvr32 actxprxy.dll

But my Mom would NEVER figure this out.

powered by performancing firefox

Save 10% storage on your Ubuntu installation

sudo apt-get remove –purge openoffice.org nautilus-cd-burner openoffice.org-base openoffice.org-calc openoffice.org-common openoffice.org-core openoffice.org-draw openoffice.org-evolution openoffice.org-gnome openoffice.org-gtk openoffice.org-impress openoffice.org-java-common openoffice.org-l10n-en-us openoffice.org-math openoffice.org-writer   openoffice.org-l10n-en-gb openoffice.org-l10n-en-za

Immediately save 10% of your used disk space.

Yes, I am storage cramped in this day and age of 750G hard disks.  Yes 250MB is not much.  I don’t care.  It also means I don’t have to download and install updates on all these packages.  I probably should have done a server install.

powered by performancing firefox

Code Better is having a book giveaway.

I like the look of the books, and I’ve referenced Code Better before.  So consider this my entry.

Palermo is blogging about NHibernate again.  It is great to see on Code Better.

I don’t know if Vista upgrade cupons are this soon or not, but I fear it may mean that I’ll want a new computer sooner rather than later.  Of course, I’d be converting the computer to run Ubuntu Linux and run the Windows in a VM.

I finally upgrade my disk in my old computer.   I still don’t know how I was living with a 15G / partition.  It was rough.  I haven’t done such an upgrade in a number of years.  I’m not sure, but I think it was smoother.  I think udev does some magic or something.  I rsynced my / to /newdisk and changed my fstab on the new disk.  fstab maintenance is MUCH smoother using UUIDs and LABELs rather than devices.  In my case I also rearanged cards on my PCI bus so that I could install grub on the SATA drive.  Grub didn’t like the 3ware card, so I’ve been using lilo for the past few years.  I was expecting some problems because sdc was to become sda, but DUH, the module load order didn’t change in my initrd, so sdc did not become sda and it would not have mattered anyway thanks to UUIDs in fstab.  The speed of the 160G SATA disk is so much more than the old 30G disk that it is almost like having a new computer.

powered by performancing firefox