DevDays 2009 day 1 C# 4.0 / The Future of C#

A small introduction for these posts you can find here.Today the first day of www.devdays.nl. the day started great with a broken Tom-tom and me taking a wrong turn in The Hague. So I missed the keynote.

First session "C# 4.0 / The Future of C#" http://tinyurl.com/qczdjs by Krishnan Subramanian. Krishnan likes to type code while presenting, so it promised to be a fun session. Krishnan starts with a history lesson:

  • C# 1.0 first managed Code
  • C# 2.0 Generics
  • C# 3.0 LINQ
  • C# 4.0 Dynamic programming

trends now in C# declarative, dynamic, concurrency

We are programming very declarative, this creates a lot of noise. Finding out what the code does can be hard. A lot of details go into the how instead of the what. Krishnan uses linq to show that linq already has less noise. His non-technical girlfriend can understand a linq query.

Krishnan says that there is room for static and dynamics languages.

When you look at hardware trends,more and more multi processors machines are getting mainstream. C# 4.0 will give more possibilities for parallel programming. Krishnan demo’s this by using a LINQ ray tracer program this program is part of the parallel extensions. To make it parallel he only uses the .asParralel keyword on a LINQ query.

The themes for C# 4.0 are

  • Dynamically typed objects
  • Optional and Named Parameters
  • improved COM interoperability
  • Co- and Contra-variance

For some parts of the programs we write statically typed objects can get in your way. For these parts you can use dynamic typed features of the DLR. The DLR provides for expression trees, dynamic dispatch and call site caching. As an example Krishnan shows ugly C# reflection to determine a type and invoking a method and compares it to JavaScript and  to c# use the dynamic keyword. The dynamic keyword is a much cleaner piece of code. In the demo he calls an iron python coded calculator form c# using the dynamic keyword. The add function can take any argument that exposes the + operator to function correctly, any argument compiles. The next demo shows writing a dynamicbag that is a child implementation of DynamicObject. In this demo he uses a dictionary that is holding the properties you can call on the object, these methods than can be added at build time from the calling code. So DynamicBag.MyCustomProp = 1 will create an entry in the dictionary the holds MyCustomProp and value 1, so this code works with some minor overriding of methods even if DynamicBag doesn’t have a property MyCustomProp.

The current way to use methods with less or more parameters is to use overloads. In C# 4.0 you have named and optional parameters. The optional parameter feature you can use by setting a default value to the parameter in the method declaration. When calling the method you can use named parameters this makes the code more readable.

For COM interop there are improvements:

  • Dynamic mapping
  • Optional and named parameters
  • Indexed properties
  • Optional ref modifier
  • interop type embedding

To explain Co- variance you can look at an array of strings and putting a button in it. This is co-variant but not safe. When looking at List<String> and Ienumarble<object> these cannot be cast to each other. In C#4.0 you can now use IEnumarable<in T> and IEnumarable<out T>.

In the future it might be possible to use the compiler as a service. Your program should be able to change compile behaviour.Krishnan shows a demo of code that will compile in the C# version that will come out after C# 4.0. In this he uses a CSharpEvaluator class with which he writes a program from strings. Using this he writes a command line c# interpreter. Kewl.

About Kees Dijk

Husband, father and Software Architect at Sogeti. Loving my work and having fun while doing it. Interested in all the aspects of software development with a special love for Model Driven Development, Code Generation and Software production lines.

Published by

KeesDijk

Husband, father and Software Architect at Sogeti. Loving my work and having fun while doing it. Interested in all the aspects of software development with a special love for Model Driven Development, Code Generation and Software production lines.

One thought on “DevDays 2009 day 1 C# 4.0 / The Future of C#”

Leave a Reply

Your email address will not be published. Required fields are marked *