Share via


C# Anonymous methods, lambda expressions and Ruby

In my last post I had discussed about anonymous methods.

I had used the following code snippet to show how anonymous methods can be used to print out a List<string>

 List<string> nameList = new List<string>();nameList.Add("Abhinaba");nameList.Add("Kaushik");nameList.Add("Samrat");

nameList.ForEach(delegate (string str) { Debug.Print(str);});

jeswin's comment on that post was "the ugliness is really apparent at the callsite ... Once we see lambdas and automatic type inference in C# 3.0, a lot of people will start using it. With LINQ, at least this is as concise as Ruby. "

He hit my sweet spot about Ruby and how languages like C# are poaching into its domain. Lets see how I'd code this is Ruby

 $nameList =  [  "abhinaba", "kaushik", "samrat"  ] 
$nameList.each  {   | str |  puts str  } 

This is concise and intuitive as for each element run this block of code. But with C#3.0 (LINQ) coming up, this can also be done as concisely with the usage of lambda expressions. The following code is valid C# 3.0

 List<string> nameList = new List<string>{ "abhinaba", "samrat", "kaushik" };nameList.ForEach(str => Console.WriteLine(str) );

So the ugly delegates and explicit type parameters have gone away. This is concise but is it really understandable by C#'s target audience?

One more interesting thing about the code is the first line where I was able to initialize the collection by directly passing the elements using the C# 3.0 Collection Initializer.

However, I am not a great fan of adding language features like this in C#. Every language has a style and a target audience and I don't think dynamic/functional programming concepts creeping into C# is a healthy trend.

Comments

  • Anonymous
    August 23, 2006
    My biggest concern is that as features are added to C# that they may not be consistent with the original balance of the language design.  While each new feature is cool, does it work well with the overall system, or does the new feature result in awkwardness elsewhere?
  • Anonymous
    August 23, 2006
    I like that C# was less didactic and purist OO than Java from the first release.  It really has a nice balance of consistency and expressiveness (meaning choices about how to code things to most closely mirror the semantics of what's being coded).  I don't think the language should be afraid to support more than one paradigm of programming.  Some are already shuddering at this implicit reference to Stroustrup's C++ philosophy, but certainly C# is cleaner and more readable than the dark corners of C++.  Language features offer choices -- not requirements.  If it makes the most sense to be able to write a loop as a lambda then it's nice to be able to do so.  Writing it this way says to the reader, "The loop is not what's important here, just digest that we iterated and printed each element."  You actually have code that is more readable because what is uninteresting semantically is no longer present syntactically.  Furthermore, there is less boilerplate code to write, which is always a good goal.  And finally, less code that is easier to write and easier to read and more semantically expressive -- all that adds up to easier to maintain.

    Perhaps the issue of "audience" here is a fear of introducing less-mainstream-than-OO concepts into the language.  But perhaps also the philosophy should be that there is room to learn and grow with C#, and that there is more than one audience.  I hope that the fact that there is one CLR under all .NET languages doesn't mean they all must gravitate to one common denominator or limit themselves to the capabilities of one percieved audience.  
  • Anonymous
    August 24, 2006
    I'm concerned about these upcomming changes in C#. I thought that current C# audience is not prepared enough to something like that.
  • Anonymous
    March 18, 2008
    In ruby example, the '$' is not necessary makes the variable "global".