Official C# 2.0 Spec Unveiled

10/23/2003 6:44:08 PM

Official C# 2.0 Spec Unveiled

Today, the C# team published the most up-to-date specification of the C# language, which includes many improvements over the original proposal unveiled a year ago.

The features introduced in the original proposal were noteworthy for including constructs from nontraditional languages like closures (aka anonymous methods) and coroutines (aka iterators) in addition to generics. Since then, the C# team has received a variety of comments, and made a number of changes to that proposal. For some reason, they wanted to keep the changes secret; perhaps, things were still in flux and they did not want to commit to the exact syntax until PDC (Microsoft's forcing function).

The new C# website is up now, along with the C# 2.0 specification.

These new features greatly increased the expressiveness of the language. The impact of generics is obvious. The other two features, anonymous methods and iterators, no longer tie local variables to a stack frame. For coroutines, we no longer have to resort to threads or fibers (described in MSDN article). The compiler support of iterator should be cheaper than either of those two alternatives. In a later posting, I'll show how iterators can be generalized, without further compiler extensions, to support continuations--essentially "gotos" across functions. (Ever wonder why academics despise gotos within functions but praise gotos across functions. Perhaps, because there are no other alternatives to accomplish the latter.)

A good overview of the new C# features using the original syntax can be found here. Here are the highlights of the current changes from the original proposal:

The implementation of Generics has already been discussed in MSDN Magazine. I won't say much on this. Generics can be applied to classes, structs, interfaces, delegates, and methods. They also support constraints. There exists a whole new collection namespace for System.Collections.Generics. It's likely the new Longhorn APIs will also utilize Generics. Also, there is a Nullable<T> type, that allows any valuetype to accept a null value, just like areference class and a new syntax T.default to indicate the default value of a type (a zeroed valuetype or a null value).

Iterators provided a simplified method of creating enumerators. The original version of Iterators required a foreach method be declared in the class. This syntax limited iterators to one per class and did not support arguments.

The new syntax uses the same function syntax as other methods, allowing for multiple types of iterators per class and any number of function arguments. The only requirements is that a yield instruction exists in the function and that the function returns IEnumerator, IEnumerable or generic versions thereof. The default iterator used for foreach must be given the name GetEnumerator. The yield syntax has been updated to yield return value to return the next value or yield break to indicate completion.

Anonymous Delegates
Anonymous methods are similar to regular methods, except they can be unnamed and inlined. Also, unlike regular methods, these are true closures: Local variables of the containing function can be captured, in which case the lifetimes of these variables are detached from stack and linked to the lifetime of the method.

The new syntax is more compact, omitting the awkward delegate constructor syntax:

Anonymous Delegates without parameters can be specified as delegate { code } .
Anonymous Delegates with parameters can be specified as delegate(arg1, arg2) { code } .

Delegate assignments is more relaxed, and can simply be assigned the name of a method.
Button.OnClick += OnClickHandler;

Partial Classes
Partial classes allow the same class can be implemented in multiple files. I don’t believe there is any changes from original proposal.

The new features are covered in detail by the new C# 2.0 specification. In addition, specification also details the possible implementation of the both iterators and anonymous methods more closely. Iterators are implemented as state machines and all local variables in the iterator are hoisted within a separate object. Simple anonymous methods are implemented as ordinary methods (named as __AnonymousMethod$0001, for example), while those capturing locals require the addition of an anonymous class. The specification also hints at code sharing between similar anonymous delegates.






Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media