PDC: C# 4.0 will go dynamic
Anders Hejlsberg, lead architect of C#, the .Net programming language, has introduced new language functions for C# 4.0 and its subsequent version at the Microsoft Professional Developer Conference (PDC) in Los Angeles.
The main innovation in C# 4.0 is its extension towards joining the dynamic languages. C# developers will in future be able to use the
dynamic keyword to declare a variable so that its binding does not take place until run time. This substantially simplifies the use and integration of COM components, because there is no requirement for type conversion or for the laborious .Net reflection library. Hejlsberg stressed however that developers should not now declare all variables in general as dynamic, but should continue to rely on static typing as standard, because this is faster. The
DynamicObject classes and the
IDynamicObject interface work "underground", and a developer can implement his own dynamic types on this basis. Hejlsberg emphasized that the dynamic extensions made C# even more recognisable as a multi-paradigm programming language.
C# 4.0 also introduces optional and named parameters. These forms of parameter have been available in Visual Basic since the first version of .Net, and have been sorely missed by C# developers. Until now, when accessing the Microsoft Office Object Libraries classes, developers have had to replace an optional parameter with an instance of the
missing class, generating multiple lines of code which VB.NET could express in half a line of code.
Further innovations in C# 4.0 concern covariance and contravariance for generic types as well as options for embedding interfaces from interoperability assemblies in the calling component (type
embedding). This function greatly eases programming for different versions of a software component. Microsoft has Office programming in mind for this functionality as previously developers had to program for a specific version of the Office library, causing errors if the user had not installed the expected version of Office.
C# 4.0 will appear as part of .Net Framework 4.0, which is expected in late 2009. The first examples should be available on Microsoft's web site in the next few days. These examples are capable of running with the pre-release version of Visual Studio 2010, which is being given to those attending the PDC and will also be publicly available shortly.
In his presentation to the PDC, Hejlsberg also predicted a major focus for C# 5.0. His team, he said, had begun reimplementing the C# compiler as managed code. For historical reasons, it was still written in C++. The new implementation, he explained, was designed to provide a programming interface that allowed the compilation process to be influenced on many levels. He showed in his presentation an example of code that compiles individual C# command lines, enabling C# to be used as an interactive interpreter language. He also mentioned "declarative programming" and "parallelisation" as research areas, but did not state any specific functions for them.