Do not name a class the same as its namespace, Part Two

(This is part two of a four part series; part one is here, part three is here.) Part Two: Machine-generated code: You write namespace Foo{  public sealed class Foo  {    public string Blah(int x) { … }  } } You take this code and run a third-party “decorator” tool over it that makes your class…


How Many Passes?

Large bodies of code written in the C/C++ languages typically divide up the code into “header” files, which are just declarations of methods and types (and definitions of macros). The actual bodies of those methods and types are in completely different files. People sometimes ask me why doesn’t C# need header files? which is a…


The Purpose, Revealed

Interestingly enough, no one correctly guessed why I needed code in the compiler to transform a method argument list into a form that batched up the side effecting expressions, stuck their values in variables, and then called the method with the side-effect-free variable references. There certainly were some fascinating ideas in the comments though! The…


The void is invariant

[UPDATES below]  A while back I described a kind of variance that we’ve supported since C# 2.0. When assigning a method group to a delegate type, such that both the selected method and the delegate target agree that their return type is a reference type, then the conversion is allowed to be covariant. That is,…


What does the optimize switch do?

I was asked recently exactly what optimizations the C# compiler performs when you specify the optimize switch. Before I answer that, I want to make sure that something is perfectly clear. The compiler’s “usage” string is not lying when it says: /debug[+|-]     Emit debugging information/optimize[+|-]  Enable optimizations Emitting debug information and optimizing the generated IL…


The Stack Is An Implementation Detail, Part Two

A number of people have asked me, in the wake of my earlier posting about value types being on the stack, why it is that value types go on the stack but reference types do not. The short answer is “because they can”. And since the stack is cheap, we do put them on the…


The Stack Is An Implementation Detail, Part One

I blogged a while back about how “references” are often described as “addresses” when describing the semantics of the C# memory model. Though that’s arguably correct, it’s also arguably an implementation detail rather than an important eternal truth. Another memory-model implementation detail I often see presented as a fact is “value types are allocated on…


Representation and Identity

(Note: not to be confused with Inheritance and Representation.) I get a fair number of questions about the C# cast operator. The most frequent question I get is: short sss = 123;object ooo = sss;            // Box the iii = (int) sss;         // Perfectly jjj = (int) (short) ooo; // Perfectly legalint kkk…


Subtleties of C# IL codegen

It must be CLR week over at The Old New Thing because it’s been non-stop posts about C# lately. Raymond’s last two technical posts have been about null checks and no-op instructions generated by the jitter when translating IL into machine code.   I’ll comment on both posts here, but I want to get the no-op discussion…


Why are base class calls from anonymous delegates nonverifiable?

I’m still learning my way around the C# codebase – heck, I’m still learning my way around the Jscript codebase and I’ve been working on it for nine years, not nine weeks. Here’s something I stumbled across while refactoring the anonymous method binding code last week that I thought might be interesting to you folks….