.NET 3.0 and C# 3.0

Friday, November 10th, 2006

.NET 3.0 was released earlier this week, to very little fanfare. I haven’t done much to prepare for it, and from what I’ve read so far there’s not much reason to unless you’re already building applications for Vista. The Windows Presentation Framework looks neat, but not something I’d use at work, where I do nearly all of my .NET development.

What really caught my eye as I was reading was LINQ, one of the upcoming features in C# 3.0 (which is not included in WinFX 3.0 .NET 3.0, and won’t be released for a while; confused yet?). Here’s a little code sample from the website which illustrates what it’s all about.


using System;
using System.Query;
using System.Collections.Generic;

class app {
  static void Main() {
    string[] names = { "Burke", "Connor", "Frank",
                       "Everett", "Albert", "George",
                       "Harris", "David" };

    IEnumerable expr = from s in names
                               where s.Length == 5
                               orderby s
                               select s.ToUpper();

    foreach (string item in expr)
      Console.WriteLine(item);
  }
}

This is just a simple data query, but what’s exciting is that it’s built into the language itself, and you can use it on any object which implements the IEnumerable interface. Here it’s an array, but it could just have easily been an MS SQL database or an XML file. Data handling has always been one of .NET’s strengths compared to other languages and platforms, and with stuff like LINQ in the works I don’t think that’s going to change any time soon.

There are 4 comments in this article:

  1. 10/11/2006Scott Stevenson said:

    NSPredicate.h adds category methods for NSArray and NSMutableArray:

    -filteredArrayUsingPredicate:
    -filterUsingPredicate:

    I think the results are very similar.

  2. 10/11/2006Marc Charbonneau said:

    I’m a big fan of NSPredicate (there have been many times when I wish I had something similar that worked with .NET’s List<T> class), but LINQ is really much more than just filtering. The aim isn’t just to work with the .NET collection classes or SQL, but any data source, whether it’s XML, an SQL database, ldap, and so on. Any query logic that’s specific to those data sources is abstracted through LINQ, and you can also use your own delegate methods to extend any part of the query (say, to modify values before they’re returned in the query). There’s more to it than just searching though; LINQ also does sorting, grouping, aggregating results into a single value as well as splitting them apart. If I’m understanding it right, you can even specify joins on different types of in-memory data objects, by specifying which attributes are related.

    I’ve heard good arguments for and against this kind of abstraction, and I don’t know how much of it will be practical in the real world. LINQ has definitely caught my interest though. I’m going to have to spend some time reading more about it once it’s officially released.

  3. 11/11/2006Scott Stevenson said:

    Without doing an in-depth analysis, I believe virtually everything you describe here can be done with NSPredicate + NSArray or NSPredicate + Core Data.

    Core Data shields you from the data source-specific logic, and you can certainly use tranisient attributes (amongst other things) to modify values before they’re returned in the query. Grouping, sorting and aggregation all works as well.

    even specify joins on different types of in-memory data objects, by specifying which attributes are related

    Not sure what you mean by this, but Core Data does have an in-memory store type which works as any other store type. It use a managed object model to describe relationships, and the model can change at runtime.

    I’m not trying to say LINQ is worthless (all I know about it is what I read here), but there’s more flexibility in Core Data and NSPredicate than many people realize.

  4. 18/11/2007nasrin said:

    I have a program that had wrote with c#.Net 3.0, now I want to run this program in visuall studio .Net 2.0 ,but I dont know what am I doing,please guide me!