Enhancing C#

Lambda expressions and Block syntax

While catching up with a friend for (a very geeky) lunch the other day, a few ideas crystalised into another idea for the next version of C#.

The lambda expression syntax introduced in C# 3.0 is a brilliant step forward from the anonymous delegates of C# 2.0, but one thing has always irked me - that a lambda expression with no parameters needs to be written this way:

() => expression

Clearly, you need specific syntax to identify a lambda expression, but is this really the best they could do?

Macros for C# (viii): Why trigger at load?

Why am I suggesting that our macros should be triggered when the class is loaded into the AppDomain, rather than at compile time? Three reasons: Inheritance, Security and Flexibility.

Macros for C# (vii): How does it work

Having spent several entries looking at how C# macros might be used, I think it's now time to explore how they might be achieved.

In C# 2.0, all of the source code you write ends up translated into straightforward IL code at compile time. This code is stored in managed code assemblies, loaded into an AppDomain and converted into native code "just in time" for execution.

Macros for C# (v): IDisposable

Another example of a situation where the interface defines little of the full contract is the IDisposable interface. The IDisposable interface just declares a single method, but there is a substantial amount of work required for a full implementation.

Macros for C# (iv): INotifyPropertyChanged

As we have seen, one of the benefits of writing a macro is that it defines behaviour as well as a calling contract. Combining macros and interfaces gives some very useful capabilities.

Macros for C# (iii): Reuse

In our previous installment, Assignability we showed how a short macro block might improve our Person class by reducing the amount of manual maintenance required as that class was modified.

Macros for C# (ii): Assignability

In our introduction, we looked at a trivially simple case of using the new macro syntax. Let's now look at a more realistic, though still simple, use.

Macros for C# (i): What is a Macro?

With the release of new features in C# 4.0, it's natural to start looking ahead to the future and speculate on what kinds of features might be added to the language with the next release. The idea of integrating compilation as a runtime service has already been raised, by Anders Hejlsberg no less, though we have as yet (late 2010) no information about what that might look like.