Saturday, January 24, 2009

C# 4.0 - the first look at the DYNAMICITY.

This week I looked at C# dynamic features that were added in C# 4.0 CTP and Visual Studio 2010. Tremendous amount of resources have appeared on dynamic features of C# over the web. C# developers provided the description of dynamic features on their blogs, i.e. Sam Ng’s blog and Chris Borrows’ blog have the great posts about the topic. However, not all the dynamic features are available in C# 4.0 2008 CTP. I.e. a lot of samples listed in the mentioned blogs don’t event compile in the current VS.NET 2010 preview. So I didn’t dive in the specific language constructions and tricks. What makes me excited is step forward to “dynamicity” in .NET. I never worked with dynamic languages before but apparently it’s about time to learn more about the dynamic languages facilities. Well, I found MSDN magazine article called CLR Inside out: IronPython and read the beautiful thing:

The most common question I hear about dynamic languages is "why should I use them?" Trying to show a longtime C++ or C# developer that a dynamic language is useful for problems outside the scripting domain is difficult. One concrete benefit of dynamic languages is that they facilitate an iterative development cycle. The interactive capabilities of dynamic languages make them a better match for this type of cycle than compiled languages. Developers can focus on exploring and prototyping functionality, refactoring if necessary, and then repeating with new functionality. This allows for a more exploratory process of development than the code/build/run/debug loop that most other languages constrain the programmer to.

So, the dynamic language, well C# 4.0, will allow transition from code/build/run/debug loop to evaluation/print loop? Yes, in PDC 2008 “Future of C#” talk Anders Hejlsberg shows it in action and it looks amazing. The new language features like dynamic type, IDynamicLanguage interface, optional and named parameters also look very promising. Next week I will try to work with C# 4.0 as with dynamic language, I think the best way to do this is get some problem, solve it using C# 3.0(statically typed language), then solve it using C#4.0(dynamic language). I will write about any interesting stuff.

2 comments:

  1. It doesn't make use of the full power of dynamics, but I was very pleased with was the way that anonymous types work in conjunction with ASP.NET MVC.

    For example; when creating a link to an action you can pass in an anonymous type that has values for each of the parameters to the action.

    In code: string url = helper.ActionLink("Search", "Search for foo", new { text = "foo", page = 1, sortOn = "Name", sortOrder = SortOrder.Descending } );

    ActionLink would then work with the ASP.NET routing engine to find the proper format for the Search action and pass in the parameters for text, page, sortOn, and sortOrder.

    It reminded me of how someone would construct JSON to pass data to or around in a client script.

    ReplyDelete
  2. I think that the most wide using of anonymous types is LINQ queries. Anonymous types allow to easily build the list of whatever objects we want, for example the typical code that used in LINQ to SQL:
    Northwnd nw = new Northwnd(@"northwnd.mdf");
    var companyNameQuery =
    from cust in nw.Customers
    where cust.City == "London"
    select new {CompanyName=cust.CompanyName, FullAddress=cust.Address+", "+cust.City"+", "+cust.Country};

    Dynamic Objects also can be used in Linq Queries and you even don't need to enumerate properties and values in the query. I.e.
    IDynamicObject example
    shows that. The linq query and further code looks like:
    var custs = from c in XElement.Load(@"..\..\Customers.xml").Elements()
    select new DynamicElement(c);

    foreach (dynamic d in custs)
    {
    Console.WriteLine(string.Format("Name: {0} {1}",
    d.FirstName, d.LastName));
    }
    Where DynamicElement is dynamic object. In addition to anonymous type we can dynamically add(not define:)) the methods and indexers for dynamic object, i.e. ToXml method which returns some debug string.

    ReplyDelete