Does C# 4 optimize away namespaces in a manner that previous C# versions did not?

A little-appreciated fact is that there is no such thing as a “namespace” from the point of view of the underlying CLR type system. Rather, it’s just a convention that we say that a type that contains periods in its name is “a member of a namespace”. Logically there is no difference at all between the legal code:

namespace N
{
    class C  {}
}

and the psuedo-code:

class N.C {}

C# forces you to pretend this pleasant fiction is reality, but it is just a fiction — from the perspective of the CLR type system, of course. From the perspective of the C# compiler, of course namespaces are “real”. They just don’t correspond to anything in metadata other than a portion of the name of a type.

In short: if you make an assembly with an “empty” namespace then the “namespace” doesn’t exist at all in the compiled binary. A “namespace” only comes into existence when there is a type in the library that has periods in its name.

Now, why you would care about ensuring that an “empty” namespace has some presence in the binary form, I have no idea.

I assume previous C# versions exhibit behaviour that do not require this dummy class

Nope. Every version of C# since 1.0 throws away empty namespaces.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)