Tuesday, March 13, 2007

Yet Another Primer on New Language Features in Orcas

Microsoft's Scott Guthrie has joined the choir of bloggers* serializing items about the new language features implemented by Visual Studio vNext, a.k.a., Orcas. Most articles in these series are devoted entirely to C# 3.0, some give a nod to VB 9.0 here and there, and a very few cover VB 9.0 only. An obvious inference is that LINQ afficianados are inclined to (re)iterate IEnumerable<linqPost> rather than IEnumerable(Of LINQPost).

Scott introduces his series with this description [emphasis added]:

In addition to all the great new framework and tool features, one of the things that I think developers (of all .NET application types) will really love about Orcas are some of the new language features and improvements that are coming to both VB and C#. These language changes are going to improve our development experience in both subtle and dramatic ways, and will really improve productivity and reduce the amount of code we need to type.

Over the next few weeks I'm going to try and blog about several of these language improvements, and show how they can be used together to produce some really powerful results.

Following are links to the complete set of Scott's posts (Updated May 15, 2007):

There's an earlier item about Scott's May 2006 Using LINQ with ASP.NET (Part 1) post and a followup article, Using DLINQ with ASP.NET (Part 2). Both parts are C#-only.

One of the issues I plan to explore in the next few months is whether reducing "the amount of code we need to type" improves programmer productivity and the extent to which it affects the cost to maintain code over the application lifecycle. LINQ's enhanced query syntax clearly requires more keystrokes than using C# lambda expressions; the forthcoming VB lambda syntax will be more verbose. Obviously, VB's With ... End With construct, which isn't available in C#, is a dramatic time and keystroke saver, as is the shorthand declaration, Dim objVar as New WhateverType for C#'s whateverType objVar = new whateverType. However, if the number of keystrokes required to achieve the desired result is the criterion of language efficiency, we'd all be writing APL.NET.

*Links to Other Series on New Orcas Language Features and LINQ

Microsoft added most new Orcas language features to support LINQ. Thus the majority of serial expositions of new C# 3.0 or VB 9.0 syntax include references to or examples of one or more LINQ flavors. Here are links to recent OakLeaf items that describe other LINQ/language-related series:

Mike Dodaro's series qualifies only because future items might cover LINQ to Entities.

Following are some series (or equivalents) that don't have their own OakLeaf posts:

Update 5/29/2007: Scott Guthrie started another LINQ series on , this time covering LINQ to SQL:

This series adds VB examples to Scott's traditional C# sample code. (Thanks, Scott.) I'll add later members of the series as they appear.

Update 6/6/2007: UK developer Granville Barnett delivers this series (each of which includes a substantial amount of C# code) on the DotNetSlackers site:

Hooked on LINQ (C#) by Troy Magennis is a wiki, not a blog, but it contains more than 100 pages of C# code examples.

Kevin Hoffman's .NET Addict's Blog (C#) has several posts on LINQ topics, including a Dr. Dobbs article, "LINQ to Web 2.0," and LINQ to XAML and LINQ to SharePoint examples.

Andrew Matthews appears to have a "How LINQ Works" series in the making:

are among his several posts on C# and LINQ topics.

Alex Thissen was one of the early authors of a blog series on C# 3.0:

  1. C# 3.0 for mere mortals, part 1: Extension methods (3/26/2006)
  2. C# 3.0 for mere mortals, part 2: Implicit typing (3/28/2006)
  3. C# 3.0 for mere mortals, part 3: Anonymous types (6/14/2006)
  4. C# 3.0 for mere mortals, part 4: Object and collection initializers (7/16/2006)
  5. C# 3.0 for mere mortals, part 5: Lambda expressions (11/7/2006)
  6. C# 3.0 for mere mortals, part 6: Expression trees (missing)
  7. C# 3.0 for mere mortals, part 7: Query expressions (missing)

In part 5, Alex described his unfinished opus as "the longest running series on C# 3.0."

Microsoft UK evangelist Daniel Moth's posts aren't members of a formal series, but he suggests reading them in order:

  1. LINQ
  2. Local Variable Type Inference (and the VB version)
  3. Object Initiliasers
  4. Anonymous Types
  5. Extension Methods
  6. Lambda Expressions (and the VB version)
  7. Decomposing LINQ (includes Query Expressions)

It's encouraging to see at least some VB coverage in language-oriented posts. Daniel's "Language Enhancements and LINQ" slides for Microsoft UK's Technical Roadshow 2007 (in .pptx format) also include a smattering of VB.

Update 4/9/2007: Earlier C# 3.0 and LINQ Series

Several C# 3.0 and LINQ series originated from the LINQ May 2006 CTP. For the sake of completeness, following are some representative examples:

Sahil Malik's seven-part "Demystifying C# 3.0" sequence consists of brief ("five-minute") examples of the following C# 3.0 concepts:

  1. Demystifying C# 3.0 - Part 1: Implicitly Typed Local Variables "var"
  2. Demystifying C# 3.0 - Part 2: Anonymous Types
  3. Demystifying C# 3.0 - Part 3: Extension Methods
  4. Demystifying C# 3.0 - Part 4: Lambda Expressions
  5. Demystifying C# 3.0 - Part 5: Object and Collection Initializers
  6. Demystifying C# 3.0 - Part 6: (LINQ) Query Expression Translation (to C# 3.0)
  7. Demystifying C# 3.0 - Part 6: Expression Trees

Sahil extended his "Demystifying" approach to LINQ to SQL (then DLinq) with:

  1. An Introduction to DLinq
  2. Setting Up a DataContext Class
  3. Querying for Data using DLinq
  4. Stored Procedure and UDF support in DLinq
  5. Stored Procedures that return a Definite Shape with DLinq
  6. Stored Procedures that return a Variable Shape with DLinq
  7. DLinq: Submitting your Changes

Bill Wagner delivered the following posts about DLinq's technique for translating LINQ queries to T-SQL statements and other related topics:

  1. LINQ to SQL Introduction
  2. LINQToSQL: Translating C# to SQL
  3. LINQ For SQL Queries: Where Clauses and Database Queries
  4. LINQ for SQL Queries: Mathematics Functions
  5. LinqToSql: Joins (9/13/2006)
  6. Linq2SQL: Group By Order By, and why I prefer C# To T-SQL (9/28/2006)
  7. LINQ 2 SQL Set Operations (10/6/2006)
  8. Paging output with Linq2SQL (10/14/2006)
  9. First Commentary on LINQ vs. Python (10/17/2006)
  10. Updating Database Content using Linq2SQL (11/1/2006)
  11. LINQ to SQL: Optimistic ConcurrencyL (12/4/2006)
  12. More LINQ 2 SQL: Object Identity and Nullable Queries (2/26/2007)

Note: The two preceding lists (except Bill Wagner's dated posts) are from Scott Guthrie's Understanding LINQ to SQL Query Translations post of September 1, 2006.

Update 4/12/2007: Eric White's Query Composition using Functional Programming Techniques in C# 3.0 is an October 2006 tutorial about taking the FP approach to writing LINQ queries that's based on the LINQ May 2006 CTP. Eric says:

It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need to understand these techniques.

Eric explains query expressions, lambda expressions, extension methods, local variable type inference, object and collection initializers, tuples and anonymous types, the yield keyword, lazy evaluation, and aggregation more eloquently and in more detail than most other authors.

Update 3/19/2007: Mike Taulty's Deconstructing LINQ item offers an all-in-one-post series of 10 brief elements with "codelets" that demonstrate most new C# 3.0 language features. Mike is a Microsoft UK evangelist "involved in getting information out to developers about what's happening with the Microsoft platform through online mechanisms like newsletters, blogs, videos and through offline mechanisms such as technical sessions."

If you know of any more LINQ-based series in the making, especially posts that provide VB 9.0 code samples, please leave a comment.