Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder...
What\'s the coolest (as in the most
I think that LINQ is a major change to .NET and it is a very powerful tool.
I use LINQ to XML in production to parse and filter records from a 6MB XML file (with 20+ node levels) into a dataset in two lines of code.
Before LINQ this would have taken hundreds of lines of code and days to debug.
That's what I call elegant!
OLINQ reactive LINQ queries over INotifyingCollection - these allow you to do (amongst other things) realtime aggregation against large datasets.
https://github.com/wasabii/OLinq
I did one (a bit crazy, but interesting) thing like that just recently:
http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/
http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/
Not my design but I've used it a few times, a typed-switch statement: http://community.bartdesmet.net/blogs/bart/archive/2008/03/30/a-functional-c-type-switch.aspx
Saved me so many if... else if... else if... else IF! statements
Perhaps not the coolest, but recently I have been using them anytime I have a block of code that gets C+Pd over and over again only to have a few lines change. For instance, running simple SQL commands to retrieve data can be done like so:
SqlDevice device = GetDevice();
return device.GetMultiple<Post>(
"GetPosts",
(s) => {
s.Parameters.AddWithValue("@CreatedOn", DateTime.Today);
return true;
},
(r, p) => {
p.Title = r.Get<string>("Title");
// Fill out post object
return true;
}
);
Which could return a list of Posts that were created today. This way I don't have to copy and paste the try-catch-finally block fifteen million times for each command, object, et cetera.