RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


What's New in C# 3.0? Part 2

The release of Visual Studio 2008 updates C# to version, 3.0, with several key language enhancements and LINQ support. Part Two of this series will walk you through C#'s new LINQ support features and other time-saving enhancements.

ith the release of Visual Studio 2008, Microsoft has updated the C# language to its latest version, 3.0. C# 3.0, contains several key language enhancements that support the recently-announced Language Integrated Query (LINQ) feature. Part 2 of this series picks up where Part 1 left off, walking you through C#'s new LINQ support features and providing a code examples that illustrate how to use them.

One of the key additions to the .NET Framework is the support for Language Integrated Query (LINQ). LINQ allows you to manipulate data just like you manipulate database records using a query language like SQL. So instead of writing complex iterative loops to retrieve the data that you desire, you simply specify the data you want declaratively using LINQ and the compiler will do all the work for you.

LINQ to Objects
LINQ to Objects allows you to use LINQ to directly query any IEnumerable or IEnumerable<t> collections directly. For example, suppose you have a collection of data stored in an array and you want to be able to retrieve a subset of the data quickly. In the old way of doing things, you'd write a loop and iteratively retrieve all the data that matches your criteria. This is time-consuming as you have to write all the logic to perform the comparison, and so on. With LINQ, you can declaratively write the condition using an SQL-like statement and the compiler will do the job for you.

To understand the usefulness of LINQ to Objects, suppose you have an array of type string that contains a list of names:

   string[] allNames = new string[] {"Jeffrey", "Kirby", "Gabriel", 
                                     "Philip", "Ross", "Adam", 
                                     "Alston", "Warren", "Garfield"};
Now, further suppose you need to print out all the names in this array that starts with the letter "G". In this case, you can set up a loop and iteratively perform a comparison on each name. Things start to get more complex when you have more complex filtering rules. Using LINQ, you can specify the filter using the from clause, like this:

            IEnumerable<string> foundNames =
                from name in allNames
                where name.StartsWith("G")
                select name;
Notice that the code declares the foundNames variable of type IEnumerable<string>. You can also use the new implicit typing feature in C# 3.0 to let the C# compiler automatically infer the type for you, like this:

            var foundNames =
                from name in allNames
                where name.StartsWith("G")
                select name;
When this statement has been executed, foundNames will now contain a collection of names that starts with the letter "G". It this case, it returns "Gabriel, Garfield". You can now print out all the names in the collection, for example:

            foreach (string peopleName in foundNames)
To can also use a more complex filter, such as the following:

            IEnumerable<string> foundNames =
                from name in allNames
                where name.StartsWith("G") || name.Contains("by") 
                orderby name
                select name;
In this case, foundNames will now contain "Gabriel, Garfield, Kirby". Note that the result is also sorted.

Another useful application of LINQ is in manipulating Windows Forms controls. Suppose you have a large number of controls on your form and you want to uncheck all the CheckBox controls without setting each individually. You can use the following code to do that using LINQ:

        //---retrieve all the checkbox controls in the current form---
        IEnumerable<Control> checkBoxes = from Control ctrl in this.Controls
                                          where ctrl is CheckBox
                                          select ctrl;

        //---uncheck all the checkbox controls---
        foreach (CheckBox c in checkBoxes)
            c.Checked = false;

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date