One of the more substantive additions to the .NET Framework 3.5 and C# 3.0 and Visual Basic 9 languages was LINQ, a set of classes along with language enhancements that
allow developers to use a common library and SQL-like query syntax to work with common data stores. The initial article in this series,
An Introduction to LINQ, provided an overview of LINQ and its core pieces: the standard query
operators, the language extensions that allow for LINQ's query syntax, and LINQ providers. We also looked at some simple LINQ examples using both the standard query operators
and the query syntax.
LINQ's standard query operators - Select, Where, OrderBy, Average, and so on - can be used as if they were instance methods
of any object that implements IEnumerable<T>. For example, given a string array named FileNames we can determine how many strings in the array start
with the letter "S" by using the Where and Count standard query operators like so:
|
The Where method and Count methods look like they are members of the Array class. However, they are defined in the Enumerable class in the System.Linq namespace as extension methods on the IEnumerable<T>
interface. Also note the syntax that is used to specify the input parameter for the Where method: name => name.StartsWith("S"). This syntax is a lambda
expression and provides a shorthand notation for developers to define a function.
This installment (and the next one) explore the language enhancements Microsoft made to C# 3.0 and Visual Basic 9 in more depth. (C# 3.0 and Visual Basic 9 are, at the time
of this writing, the most recent versions of these two programming languages. They are the versions that were released with the .NET Framework 3.5 and Visual Basic 2008.)
The key language enhancements that make LINQ possible include: extension methods; implicitly typed variables; object initializers; lambda expressions; and anonymous types.
This article explores the first three of these language extensions; the latter two will be covered in the next installment. Read on to learn more!
Read More >
0 comments:
Post a Comment