Share via


Community Convergence L

Welcome to the 50th issue of Community Convergence. Now that Visual Studio 2010 Beta is out, I think it might be helpful to draw attention to the new dynamic programming and office development features from the C# team that appear in that release. The C# team defines the core syntax found in C# 4.0, and the IDE features that make it easy for developers to access those features. In this post, I’ll focus on language features, in upcoming post, I’ll focus on IDE features.

There are, of course, many new tools and APIs that will appear in Visual Studio 2010. In this post I’m focusing exclusively on core language features from the C# team itself, as opposed to features that come from the WPF, WCF, Silverlight, or other teams.

The text presented here is meant to provide a high level overview of these features. At the end of the article I will point you to more technical resources that will allow you to explore these features in more depth, and to read more precise, rigorous definitions of the new features in C# 4.0.

Language Features

Dynamic programming and enhanced interaction with Office are the main theme in C# 4.0. Dynamic programming is a means of writing code that does not rely on a static types linked at compile time. Traditionally C# has been a strongly typed language defined in large part by static types that must be declared explicitly at compile time. For instance, string, int, and object are all strongly typed static types that are resolved at compile time. A dynamic type is not linked at compile time, but is instead resolved at run time.

Why should C# developers care about dynamic programming? Isn’t the strongly typed nature of the C# language one of the language’s best features? The answer to this question is a resounding yes. Strong typing is one of the great advantages of the C# language, and the team expects most developers to continue to write strongly typed code nearly all the time. There is nothing in the new C# 4.0 features that prevents you from continuing to exclusively write strongly typed code, if that is what you prefer.

However, there are occasions when it is convenient to write dynamic code. For instance, languages such as Python and Ruby are dynamically typed. It is awkward, and sometimes nearly impossible, to call into these languages from a strongly typed language such as C# 3.5. The Office API is also difficult to call from C# 3.5; it has long been awkward for C# developers to call into Office using the current set of language features.

C# 4.0 is adding dynamic features so that it will be easier for developers to call into dynamic languages such as Python, and into more flexible APIs such as Office. You can think of the entire dynamic programming effort as a move by the C# team to fill in existing holes in the language. C# has always provided a great way to write strongly typed code, now we’re adding support for dynamic code, and for calling into Office applications.

The team has broken out the new features in C# 4.0 into four categories, all but the last of which are directly related to dynamic programming or Office development:

  • Dynamic Lookup
    • A syntax for making calls into dynamic languages and APIs.
  • Named and Option Arguments
    • Allows developers to easily call into and create methods that support optional parameters. This is a big aid to Office developers since the Office API’s make heavy use of optional parameters.
  • COM Interop
    • Language features that make it easier to call COM objects. When calling COM objects you can now omit the keyword ref, you need to make fewer casts, and you no longer need depend on heavy-weight files called Primary Interop Assemblies, or PIAs.
  • Variance
    • Covariance and contravariance have nothing to do with dynamic programming or Office development. Instead, they represent a small, very technical enhancement to the language that makes it easier to use inheritance when writing generic code. In general, this feature ensures that the language behaves as expected, rather than forcing you to rewrite code that looks like it should compile. For instance, the following code has a natural syntax that looks like it should work, but it does not work in C# 3.5. The upcoming changes in C# 4.0 ensure that it will compile:

      IEnumerable<string> strings = new List<string>();
      IEnumerable<object> objects = strings;

That’s the end of our quick overview of the new features in the C# 4.0 language. Again, remember that there are many other new features of in Visual Studio, and many new enhancements to existing technologies such as WPF, WCF, Silverlight and LINQ. In this post, I’ve focused exclusively on changes to the basic syntax of the language that have been implemented by the C# team itself.

References

Here are few references for people who want to explore this subject in more depth. A guide for developers interested in all things related to C# 4.0 is the C# Futures site on Code Gallery. There you will find a downloads page that contains the following key resources:

  • An excellent and definitive white paper on C# 4.0 language features. Mads Torgersen was the primary author of this document.
  • The C# 4.0 samples

Other important resources include C# 4.0 sections Eric Lippert’s and Sam Ng’s blogs:

kick it on DotNetKicks.com

Comments

  • Anonymous
    June 22, 2009
    You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Anonymous
    June 22, 2009
    IList<string> strings = new List<string>(); IList<object> objects = strings; This is unsound IList<> must be invariant

  • Anonymous
    June 23, 2009
    Thanks Nick. That's a good point. I've updated the code to work with IEnumerable rather than IList. This new code works because there is no Add method for insertions included with IEnumerable. This means you can't try to insert a another type, such as an int, to the sequence of objects.

  • Anonymous
    June 23, 2009
    I guess you meant "C# 3.0" instead of "C# 3.5"

  • Anonymous
    June 27, 2009
    Hi Charlie! Thanks for sharing! I have also written an article on some of the C# 4.0 Features. To read the article, please visit : http://www.codeproject.com/KB/cs/CSharp4_Features.aspx