Dynamic typing in C#

Erik Meijer is back today with an overview of how some of the concepts in the talks of the other speakers can be done in C#, although here and there a dirty hack is necessary, but “I love hacks” says Erik.


Comparing C# and Haskell: an algebraic datatype can be created simply with C# with dynamic casting.

Type inference too is possible in C#: However, with constructors this is not possible, since C# supports overloading with multiple parameters. The trick is to use “phantom methods” that you pass the default type so the compiler can infer the type.

Now, Erik says, he is going to make Simon Peyton Jones cry: What is the type on an anonymous type? It is impossible to express, but wel can create instances of it!

Now, we can create a map and do dynamic casting on either cons or nil.

Who needs recursive lists? We can just use a lazy list created with a default List of int.

GADT’s are also easy in C#, actually the come for free, by creating a subtypes that correspond to the constructors.

Parallel LINQ (like par in Scala) Just by adding .asParallel() on a list, execution will be paralelized. And C#’s ints are of course bigger than Pythons.

“You don’t need Haskell or Scala or Python, just come into my warm arms and program C#”

Events in C#

Erik wants to monitor the temperature of his wine cellar with a thermometer. He will implement this with C#. If the temperature is too high, he want to get an sms (this is done with Twillio) In this lecture he will talk about how to handle the JSON  that Twillio sends (In the next talk, he will tell us about how to work with asynchronous tasks)

The returning JSON is parsed and accessed as a hashtable.

dynamic jsond = json;

When ‘var’ was introduced in C#, many people rebelled and said that they did not want dynamic typing. But! var is not dynamic typing, the type is inferred. Then there is also the type dynamic. This is a static type to indicate that you want dynamic binding. This is the same as writing object in VB (so it is not new, VB had this for ages!) It differs from object in javascript because an object will not have the needed methods. In C# you stay in dynamic mode and you “have to cast your way out” In this example, to DateTime. This cast can be done implicitly too, by assigning to a type DateTime.

Static typing where possible, dynamic typing where necessary. “You don’t have to leave the warm arms”

So how does the dynamic resolution work?

  • Try to resolve statically (.ToString(), .Equals())
  • If is implements IDynamicMetaObjectProvider, it will be asked to to the binding yourself
  • If your object is a COM object, the operation is disptached dynamically
  • If it is a standard .NET method, reflection will be used on its type and the C# runtime binder.

With dynamic, you can do everything: method calling, getter and setters, calling operators, invoking as delegates, calling a constructor. It is just an ordinary value.

Because the JsonValue implements the IDynamicMetaObjectProvider, this is why the json statements above worked. After the break asynchronous programming.

This post was visited 452 times.

1 Comment

  1. Pingback: What is the type of an anonymous type? | My .NET Corner

Comments are closed.