Tuesday, September 23, 2008

Python has very useful constructs for accepting variable number of positional and keyword arguments:

def f(*args, **keywargs): pass

The arguments are collected as a tuple and a dictionary. C# can collect an arbitrary number of arguments to a method (no messy functions in this language!) as an array (with the params keyword) - but it has no concept of keyword arguments let alone collecting them as a dictionary.

In this blog entry Srivatsn (IronPython tester) introduces the 'ParamDictionaryAttribute' that is part of the Dynamic Language Runtime ane explains how to use it to write C# classes that can take arbitrary number of keyword arguments: