C# 4.0’s dynamicity

I just found an article ranking highly on Hacker News (my favourite read) concerning the release of C# 4.0 – you can find it on blogs.msdn.com. On this blog Microsoft claims a couple of highly sophistacated new features. Being the spoiled guy I am, they just seem natural to me. Since they started their article with the words “The dynamic keyword is a key feature of this release.”, let me demonstrate the new features in a really dynamically typed language where they have been around for quite some time: Python

Dynamic

C#

 
                        dynamic contact = new ExpandoObject();
                        contact.Name = "Patrick Hines";
                        contact.Phone = "206-555-0144";
                        

Python

 
                        class contact: None
                        contact.Name = "Patrick Hines"
                        contact.Phone = "206-555-0144"
                        

Optional (or Default) Parameters

C#

 
                        public static void SomeMethod(int optional = 0) { }
                        SomeMethod(); // 0 is used in the method.
                        SomeMethod(10);
                        

Python

 
                        def some_method(optional = 0): pass

                        some_method()
                        some_method(10)
                        

Named Arguments

C#

 
                        var sample = new List<String>();
                        sample.InsertRange(collection: new List<String>(), index: 0);
                        sample.InsertRange(index: 0, collection: new List<String>());
                        

Python

 
                        def foo(bar, foobar): None

                        foo(bar='asdf', foobar=12)
                        foo(foobar=12, bar='asdf')
                        

I honestly know that a comparison of a statically typed language on the CLR and an interpreted dynamic language doesn’t account for too much. But since Microsoft is making a fuzz about dynamic being the keyword of the new release, I felt the urge to drop this note.

The Python code was tested with v2.5 – that’s the oldest installation I’ve got. However, it’s old enough, because .NET didn’t even have decent IPC back then (i.e. Named pipes were added in .NET 3.5).

Well, that’s my rant for the night – I’m going back to teaching myself a real programmers language(Clojure) – as you should, too(;


    If you liked the article, follow me on twitter here
twitter_preek

Category: articles | Tags: , 2 comments »

2 Responses to “C# 4.0’s dynamicity”

  1. Captain Code

    You’re not ranting enough!
    More harsh language!
    ;)

  2. Minion Rush

    Woah! I’m really enjoying the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s
    very difficult to get that “perfect balance” between user friendliness and
    visual appeal. I must say you’ve done a awesome job with this.

    Also, the blog loads super fast for me on Safari. Outstanding Blog!