Wednesday, March 09, 2005 6:48 PM bart

C# 2.0 compiler enhancements

Let's try to bypass the hype of the VS2005 tools and show you guys some of the enhancements on the field of csc.exe, the C# compiler, in .NET v2.0.

  1. The version number

    Yeah, right. Just to mention the build I'm describing is v2.0.40607.42, which is beta 1.
  2. Strong naming of assemblies

    Remember the assemblyinfo.cs file and the attributes to put a strong name on an assembly? Basically, you needed to create a strong name using sn.exe and to alter the assemblyinfo.cs file to point to the key pair you created in order to create a strong-named assembly. In C# v2.0 you have an additional way to do this kind of stuff, without using the [assembly: ...] attributes:

    csc -keyfile:thekey.snk ...

    As you'll see, registering the assembly in the GAC (one of the reasons to strong name an assembly) using gacutil.exe will work directly. Beside of the -keyfile flag, there are other flag for this purpose too: -keycontainer and -delaysign
  3. Specification of the target platform

    Maybe you've seen it in the toolbars inside VS2005 already, but now it's possible to target a certain platform (x86, x64 and "AnyCPU") when compiling to IL. In doing so, the assembly's manifest will contain a flag .corflags, set to the target platform (0x00000001 being AnyCPU and 0x00000003 being x86 for example). Notice that compilation to x64 currently does not work, since the mscorlib.dll is currently not built to target that platform (will change of course) and this file is referenced to during compilation.
  4. Dropped - Incremental compilation

    The /incremental switch has disappeared. As I did rarely use it, I won't miss it (I suppose).
  5. warnaserror extension

    In csc 7.x targeting .NET Framework v1.x, there was already the option to do /warnaserror+. However, by doing so, all warnings were treated as being errors (therefore causing termination of the compilation when warnings are found). Now it's possible to specify that a specified list of warnings need to be treated as errors, whileas others remain just "warnings".
  6. Errorreport - hopefully you don't need it

    Used to send error reports about compiler errors (that is, errors in csc.exe itself) to Microsoft, specifying the mode to do this (none, send, prompt).
  7. Aliasing when using references

    Using this

    csc /r:SomeAlias=MyAssembly.dll Hello.cs

    allows you to write this code

    extern alias SomeAlias;
    public class Hello
    {
         public static void Main()
         {
              SomeAlias::Fully.Qualified.ClassName.StaticMethodInClassName("blabla");
         }
    }

    Right, two C++ "things" (extern and ::) have come to C# :-).

Another nice one - Conditional compile symbols (works in C# 1.x too, but I'd like to mention it because I like it so much :-))

You know #if and #endif, the preprocessor directives that can be used to include code (or not) based on some preproc "variable"? People who've done C and C++ certainly know this (#include, #define, #ifdef, etc), and it's available in C# too. However, in order to set such a preproc variable, you needed to jump in your code file in order to define the variable using the #define instruction. Let's give an example:

//compile with csc /define:WORLD Hello.cs
//or add #define WORLD in the code

using System;
class Hello
{
 public static void Main()
 {
#if WORLD
  Console.WriteLine("Hello World");
#endif
 }
}

Use of targets file (MSBUILD)

In the .NET Framework installation folder (Windows\Microsoft.NET) you'll now find a file called Microsoft.CSharp.targets which contains the XML description of the build process and everything around it, as used by MSBUILD and the tools. In this file, you'll find a tag, setting various variables on the "attributes of the compiler" (which are mapped to flags when using the command-line compiler directly of course). If you want to compare this system to existing similar technologies, you can compare it with makefile and ant-things, but much smarter :-). Note there is a Microsoft.Common.targets file too, that is included in the CSharp targets file. A full elaboration of these files would be outside the scope of this post, but be sure to check out these files (also when you're interested in ClickOnce, as there are targets such as "ComputeClickOnceManifestInfo" included in this file to support ClickOnce (which means once again, that you can do ClickOnce deployment without using the VS2005 tools directly).

ILdasm - No more tricks

Few people knew it, the /adv flag of ildasm.exe v1.x. It's not in the help, it's not in the /? information of the tool. The idea was pretty simple, use ildasm /adv and you get more menus, to view the PE (portable executable) and CLR headers of a file, statistics, etc. Now, it's there by default, no more /adv flag needed.

ILasm would bring us too far in the context of this post, but if I find the time, I'll write something about it. One of the nice new additions is the generation of Edit-and-Continue deltas for E&C support. This proves that the Edit & Continue support is on the level of the .NET Framework (IL-code level) and thus it can be supported for any language (E&C support is coming indeed for both VB and C#).

Ngen - extra options

Ngen (native image generator) has some new options, for example, to defer the creation of native images till the computer is idle (especially interesting for large assemblies). Additionally, the various actions are better described right now (install, update, finish, uninstall, display).

What about VB?

Well, one of the remarkable new features is the support for documentation generation using the vbc.exe compiler (and in the VB language generally spoken). This was already supported on csc (using the /doc flag), and now it's there on VB too. As I'm not a huge VB user (anymore), I can't really tell you everything about the new VB features, but there are various sources around the net to tell you more about this.

Let's stop for now; I'll come back to this topic later on.

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

No Comments