Steve Michelotti
Site Sections
Home
Contact
Syndication
Login
Posts
208
Comments
1144
Trackbacks
51
<< CMAP Presentation - C# 3.0 Language Features
|
Home
|
EntLib Validation Application Block - Validate a string in ASP.NET >>
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
.
Share This Post:
Short Url:
http://wblo.gs/VjG
posted on Wednesday, May 7, 2008 4:27 PM
Print
Comments
#
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
#
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
#
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
Remember Me?
G
o
o
g
l
e
My Blog
Archives
June 2015 (2)
May 2015 (1)
April 2015 (1)
February 2015 (1)
October 2014 (1)
September 2014 (1)
June 2014 (1)
May 2014 (1)
March 2014 (2)
January 2014 (1)
September 2013 (2)
July 2013 (1)
June 2013 (1)
May 2013 (1)
April 2013 (1)
February 2013 (2)
November 2012 (2)
October 2012 (3)
September 2012 (2)
July 2012 (1)
June 2012 (3)
May 2012 (1)
April 2012 (1)
December 2011 (1)
November 2011 (1)
October 2011 (1)
August 2011 (2)
July 2011 (2)
June 2011 (2)
May 2011 (3)
April 2011 (2)
March 2011 (2)
February 2011 (1)
January 2011 (1)
December 2010 (1)
November 2010 (5)
October 2010 (3)
September 2010 (2)
August 2010 (4)
July 2010 (1)
June 2010 (6)
May 2010 (8)
April 2010 (7)
March 2010 (4)
February 2010 (2)
January 2010 (3)
December 2009 (2)
November 2009 (9)
October 2009 (8)
September 2009 (2)
August 2009 (12)
July 2009 (3)
June 2009 (2)
May 2009 (3)
April 2009 (3)
March 2009 (3)
February 2009 (3)
January 2009 (4)
December 2008 (2)
November 2008 (3)
October 2008 (5)
September 2008 (2)
July 2008 (2)
June 2008 (6)
May 2008 (2)
April 2008 (3)
February 2008 (1)
December 2007 (5)
August 2007 (5)
March 2007 (1)
February 2007 (2)
November 2006 (2)
July 2006 (1)
June 2006 (3)
March 2006 (2)
February 2006 (2)
January 2006 (5)
December 2005 (3)
Post Categories
Game Development
General Interest
Installshield
Reviews
Heroic Adventure!
NINE Questions
Urban Exploration
Dark Sun
Travels
Electronics Hacking
bitcoin
litecoin
cryptocurrency
financial
speaking
user groups
microtransactions
coinbase
github
release
Iot
Alexa
Amazon
SmartThings
MonoGame
Code
Code Samples
Publications
Channel9 - 64-bit Computing for High Performance Web Solutions
Channel9 - Step into the New Decade with C# 4.0
MVC - Partition Web Apps Intelligently
Nullable Data Readers
Screencasts
Validate Business Objects
Tag Cloud
General
PowerShell
Tools
Copyright © 2005 Steve Michelotti
This work is licensed under a
Creative Commons License