New Features of C# 4.0

C# was created by Microsoft to become the main language of their .NET framework. It is a simple language, following the tradition of Java in many aspects, but it has a particular feel that makes it really cool to use. Moreover, although it was created by Microsoft, C# can now be used successfully in other systems, such as Linux — thanks to the Mono guys.

In this article, I want to share with you some of the features that are being integrated into C# for its next major release, version 4.0 that will be available with the next version of visual studio. There is a lot of stuff being cooked on the Redmond labs, but the most visible features are related to dynamic method invocation and parameter passing.

Dynamic Programming in C#

The main feature that will be available for C# 4.0 is access to code created on non-typed programming languages such as Python and Ruby. The mechanism that provides this functionality is the support of the compiler and runtime, designated by the dynamic keyword. By using the dynamic keyword instead of providing a statically defined type name, you are saying to the C# compiler that the object is defined at run time, and is able to respond to dynamic messages.

For example, suppose you want to instantiate an object create by Iron-Python (the .NET-enabled version of Python). You can send messages to the object using code similar to the following:

dynamic employee = getEmployee();

The important part of this code is “dynamic employee”, where the variable employee is declared as being of a dynamic type. What this says to the C# compiler is that the type of employee is unknown at compile time, but that it can dispatch dynamic messages sent to it.

Therefore, in the second line, the method call calculateSalary is not resolved at compile time. Instead, the call to calculateSalary is resolved only during execution. This way, a dynamic run time will have the opportunity to receive information about the call, such as its name and parameters. With that information, a particular run time back end (such Iron-Python) can call the right function, and the program can continue.

With this new feature, C# becomes more useful as a language that can connect to statically typed as well as dynamically executed languages. In the new version of C#, this feature will be used to provide special ways to handle XML and SQL data.

Default Parameters

Another area where C# will simplify the life of programmers is in the way parameters are passed to functions. C# rules for declaring and passing parameters are similar to Java rules. In particular there currently are no default parameters to simplify message calls.

C# 4.0 introduces default values for parameters. In this way, instead of typing

class Employee {
  public void calculateSalary(double rate, double hours)
  { /*... */ }
  public void calculateSalary(double rate) {
    calculateSalary(rate, 40.0);

one can use a default parameter for hours, so that only one method needs to be declared:

class Employee {
  public void calculateSalary(double rate, double hours = 40.0)
  { /* ... */ }

Another nice feature is the introduction of named parameters. With named parameters, one can skip parameters that have default values, and name only the parameters that have non-default values. This is useful in the world of Office-scripting, where most parameters are set to default values.

Going back to our example, suppose that both parameters have default values:

class Employee {
  public void calculateSalary(double rate=1.25,
     double hours = 40.0) { /* ... */ }

Then, one can make the following method call:

Employee e = getEmployee();

The new syntax also works nicely as an additional form of documentation for the method call, so you can reduce the pain of methods with many parameters (that, if you remember , you shouldn’t have anyway). The syntax is also similar to the standard Smalltalk way of message passing.


C# is a language that is evolving rapidly. It has a great set of tools, being the primary language used by Microsoft. It is nice to know that they are trying to make the language more useful for the integration with dynamic language. Also, I hope that such features will someday get its way into Java. Although I am certainly not holding my breath.


Related Articles

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

5 Responses to “New Features of C# 4.0”

  1. Yawn …

    Objective-C has those features since the beginning, that was in …


    By LSH on May 7, 2009

  2. Yes, I know that languages like Java and C# are just trying to catch up. But its ok, one day they will be all as powerful as Lisp ;-)

    By coliveira on May 9, 2009

  3. I am using default parameter functionality in Delphi from morethen last 10 years

    By Ripal on Jul 6, 2009

  4. Thanks for the comment you left at my site for the “C# 4.0 goes dynamic” entry.
    Since then I’ve written an article on the fusion of the type systems and more on the dynamic’s keyword use such as MMD/advanced polymorphism. Link: Type systems demystified

    By Nikos on Apr 26, 2010

  5. Hello all! I like this forum, i inaugurate many gripping people on this forum.!!!

    Large Community, respect all!

    By attafSoonse on Jun 21, 2011

Post a Comment