Tag Archives: li

Understanding Types in .Net Part 1: var keyword

It’s surprising to me how many developers don’t understand the .Net type system. So I decided to write a series of posts on of some of the misconceptions.

The “var” keyword, introduced in C# 3 is implicit typing. For all intents and purposes, this is the same as strong typing. It is strongly typed at runtime. Implicit typing means that at compile time, the type of the variable is determined based on what is originally assigned to it. You can sort of think of it like a text macro in C. In that metaphor, the token var is replaced with the appropriate type. If the compiler determines that variable is a string, then you can not later assign an int to variable, the same as if you had declared it a string in the first place.

The intent was to make typing easier with some of the linq types and generics. See the following lines of code that do the same thing:

List<ICustomer<Retail>> Customers = new List<Customer<Retail>>();

// add customer here

// old way
IQueryable<ICustomer<Retail>> lateCustomers = 
                              Customers.Where(c => c.Bills.UpToDate == true);

// new way using var, 
//   c is still typed as IQueryable<ICustomer<Retail>>
var c = Customers.Where(c => c.Bills.UpToDate == true);

Here are some unit tests demonstrating that var is still strongly typed.

using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace Tests
    class TempTest
        public void VarTypeIsStatic()
            var v = "foo";
            string s = v;
            // v = 3; //This would be illegal
            Assert.AreSame(v.GetType(), s.GetType());

Hopefully, this becomes more clear to developers in .Net 4. After all, why would the dynamic keyword be added if var was weakly typed?

Developing for Open Source Languages on Windows the Better Way

When working with php, rails, python, etc, windows is sufficient, but definitely leaves something to be desired. The ports of these languages are definitely second class. With Microsoft’s backing php support with IIS is improving, but it’s not the same. And usually you are going to deploy those apps on a linux/bsd web server, so it’s nice to know if there are any nuances. A lot of developers opt for a mac if they are developing in these types of languages. But macs can be pricey, and buying a new machine to work on other platforms seems overkill.

Now I do have a mac, but have a windows 7 laptop at work, and sometimes I do work in ruby on various projects. So I have worked out a way that I think is worth sharing. At it’s core, the idea is to use a vm, but there are some other tweaks that make this method even better.

Install VirtualBox. It’s from Sun Oracle and it’s freely available.

Create a virtual machine with Ubuntu. Or your favorite flavor of linux or bsd. I used the server version of ubuntu.

Setup Networking (Optional). You can use bridge mode, but for added security I used NAT for the adapter on the virtual machine. To then access the machine locally, forward your local ssh port to the virtual machine using the following instructions.

Install Putty with Tools and XMing in Windows. Available here and here, these tools will let you do ssh with X forwarding in windows.

Setup a Connection in Putty.
connection info

Enable X-Forwaring in Connection.

Setup SSH Tunnels.
ssh tunnel

Setup a Proxy connection in Firefox

Create your user and install your language platforms in the virtual machine. For example in ubuntu, you’re looking for “sudo apt-get install ruby-1.8”.

Install your preferred editor in the virtual machineEmacs, Vim, Eclipse, Netbeans, etc.

Your environment is now setup. I’ll give an example with ruby:

  • Start the virtual machine
  • Start XMing
  • Connect with Putty
  • Start your editor (for example “emacs &”)
  • Open Firefox
  • Start your app (for example “ruby script/server”)
  • Go to http://localhost:3000 in Firefox

This virtual machine does not require a lot of software installed, and a small amount of ram (like 128mb) should be sufficient. And by backing up your virtual machine (which should be pretty small), you have backed up your entire development environment without getting the documents, media, etc that you have on the windows side.

Hopefully you find this helpful if you want to develop in environments that lend themselves to this environment.