Posts
208
Comments
1144
Trackbacks
51
CMAP Presentation - C# 3.0 Language Features Code Samples
The code samples from my recent presentation at the CMAP Main meeting can be downloaded here:  C# 3.0 New Language Features. The direct download link is here.
posted on Wednesday, May 7, 2008 4:27 PM Print
Comments
Gravatar
# re: CMAP Presentation - C# 3.0 Language Features Code Samples
Ying Jin
5/19/2008 5:26 PM
Hi, Steve,
I attended CMAP codecamp and was blown away by your multi-tier LINQ presentation. Unfortunately I wasn't able to attend the C# 3.0 presentation you had for CMAP, it must be very cool. I think LINQ is only a iceburg tip for functional programming capability of new C#.

I myself am picking up all the new features of C#3 and C#2. One question I have is: Lambda expression in C#3 is a functional-programming-sugar-coded anonymous method of C#2. So I assume I can translate all lambda expression to anonymous methods. Can I translate all anonymous method to lambda expression?

The following is a small program I am trying to convert to lambda expression from C#2 anonymous method:
using System.Threading;
class Program {
static void Main() {
for (int i = 0; i < 5; i++){
//ThreadPool.QueueUserWorkItem( delegate {
// System.Console.WriteLine(i); }, null); //C#2
ThreadPool.QueueUserWorkItem((i)=> System.Console.WriteLine(i), null);
}
System.Console.ReadLine();
}
}

But I get an error message like
Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else on line 15.

Have you ran into some problem before?
Thanks
Ying
Gravatar
# re: CMAP Presentation - C# 3.0 Language Features Code Samples
Steve
5/21/2008 2:07 PM
Hey Ying - Glad you liked the LINQ presentation.

Your example below is an interesting one. When you use anonymous methods in C# 2.0 for an example like this, it is actually generating a class behind the scenes (you can see it in Reflector) and that class has a method that matches the WaitCallback delegate (which takes a System.Object as a parameter). Rather than pass to that parameter, it actually assigns the "i" to an auto-generated member variable and uses that inside the method. If you compile only your 2.0 version with anonymous methods and open in Reflector you'll see what I mean when you see a class called something like <>c__DisplayClass2. In that case you're using the "delegate" keyword with no () and hence no parameters.

Anyway, if you want the equivalent in C# 2.0 lamdas, this line of code will give it to you:

ThreadPool.QueueUserWorkItem((o) => System.Console.WriteLine(i), null);

Although you were not able to attend my presentation on C# 3.0, have you downloaded the code samples? There is a file in there called HelloWorlds.cs which directly compares/contrasts C# 2.0 anonymous methods to C# 3.0 lambda expressions. You can download it at the above link.

Hope this helps!

Steve
Gravatar
# re: CMAP Presentation - C# 3.0 Language Features Code Samples
Ying Jin
5/29/2008 3:12 AM
Hi,Steve,
Thank you very much for the pointer. It works!

Yes, I downloaded the code and learned quite a lot from it. One side benefit from picking up C# 3.0 is that I rediscovered the C# 2.0 functional programming features, i.e anonymous method for capturing lexical variables, yield return as a means of implementing continuation. With better understanding of C# 3.0, LINQ is more than another O/R mapping tool, seems to me it also offers a path for OO guy to functional programming goodies.

Ying

Post Comment

Title *
Name *
Email
Comment *  
Verification

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud