The Misunderstood var Keyword

I find amazing how the var keyword introduced in C# 3.0 is misunderstood.

The web is full of questions asking what are the performances implications of using var, of how var is not type safe and other completely false statements.

Also, one a day, I had a discussion with colleagues that argued that if I was using var, I could also use Object as the type for all the variables and then cast everywhere.

I don’t know why the var appears so misleading. I find it very simple, and have no issues using it a lot. Now, there are some readability discussion to using var, but that is purely subjective.

Everything Happens at Compile-Time

The very important concept that has to be understood is that with var, everything happens at compile time. You make the compiler work a little more when building your assembly, but the generated IL is exactly the same than if you explicitly use types.

So, the performances are only at compile time, and frankly, who cares of the performances at compile time?

Let’s go over this again.

Look at this code and the generated IL:

static void Main(string[] args)
{
    var s = "Hello";
}
.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] string s)
    L_0000: nop
    L_0001: ldstr "Hello"
    L_0006: stloc.0
    L_0007: ret
}

Now the same code, using var instead of String:

static void Main(string[] args)
{
    var s = "Hello";
}
.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] string s)
    L_0000: nop
    L_0001: ldstr "Hello"
    L_0006: stloc.0
    L_0007: ret
}

Exactly the same IL. Exactly.

To quote MSDN:

It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.

It cannot be more clear!

So please, stop picking on the poor var. Go pick on dynamic.

Parameter Passing and Reference Types in C#

A few days ago, I had a question at work on why “regular” objects could be modified when in a method, while strings couldn’t. That’s a good question that any C#/Java developer (and many other languages) will ask at some point.

The issue there is the general misunderstanding about parameter passing in C#. My response to that is generally to say that “In C#, all parameters are passed by value”. Which is wrong if you include ref and out modifiers, but I won’t cover these in this post. I say it that way because it makes people think about it. Generally the first answer is “No because I can modify an object in a method!”.

That’s when you realize that most of the people understand the logic, but have issues being precise on telling what is truly happening. It has to do with the fact that strings are reference types in .NET. Taken from MSDN:

Variables of reference types, referred to as objects, store references to the actual data.

To get with my previous statement, the value of a reference variable is the reference to the actual data. Also, the actual data will be stored on the heap, but this is implementation details that we should not take into account.

So, to sum it up, when you pass a variable of reference type to a method, you actually pass the reference itself, as a value, to the method.

Here is very simple sample of code to illustrate all of that:

static void Main()
{
    String name = "Philippe";

    Console.WriteLine(name);

    Modify(name);

    Console.WriteLine(name);

    Console.ReadLine();
}

static void Modify(String text)
{
    Console.WriteLine(text);

    text = "Hello";

    Console.WriteLine(text);
}

And here is the printout for this:

Philippe

Philippe

Hello

Philippe

Let’s examine the memory during the different phases.

Here is the memory just before the call to Modify:

Memory1

Now, here is the memory when entering Modify:

Memory2

We can see that both text and name variables have the same value, namely the reference to a location in the heap that contains a string which content is “Philippe”.

Now, with that picture in mind, it’s very easy to imagine what will happen when we change the value of text:

Memory3

As simple as that. We modified the value of text, assigning it the reference to a string containing “Hello” somewhere in the heap. But we didn’t modify the name variable nor it’s content.

When we exit the Modify method, text variable gets out of scope and is eligible for garbage collection. The name variable was not modified in the process.

Now, this confusion also arises because of the fact that string are immutable. Mutable object’s internal content can be modified if you have a reference to them, but this does not hold true for immutable reference objects as you don’t modify them, you create new.

For a very good extensive tutorial on this topic, please see Jon Skeet’s excellent article on the subject. It’s probably much more clear that what I can explain.

How I Finally Discovered when to use Debugger.Break()

The existence of the System.Diagnostics.Debugger class is not something new to me, but I never figured out an usage for it. I never used it, and my knowledge was limited to think that it was a bit useless to code a breakpoint while you could set it in visual studio.

However, this makes quite a lot of sense in some scenarios, like programs that execute too fast, or PowerShell cmdlet that you don’t have a chance to attach to.

Related StackOverflow question: http://stackoverflow.com/questions/1432359/what-are-the-benefits-of-using-system-diagnostics-debugger-break-over-attach-to

Live and learn!