다음을 통해 공유


Anders Hejlsberg about C# 3.0 / LINQ at TechEd:Europe in Barcelona

Having been to TechEd:Europe in Barcelona last week, I've seen a lot of cool sessions about Office 2007, Sharepoint, ASP.Net Ajax, XNA and WPF.

But one of the most interesting one was a session from Anders Hejlsberg about the future of C#. As "father" of the C# language he gave an overview of the new Language Integrated Queries (LINQ), which will be available in the next version of C#.

LINQ enables developers to query data sources directly out of code with a SQL like query language. You could write

to get all customers out of a table.

This new way of queries is only possible because of several new language features in C# 3.0, which Anders explained in his 75 min. session:

  • Local Variable Type Inference
    The compiler automatically substitutes the var keyword with the corresponding type.
    So you could write a code like this:

var a = 5;
var b = a + 5;
Console.WriteLine(b);
Console.WriteLine(b.GetType());

The compiler will automatically detect, that a and b are of type Int32.

  • Extension Methods
    All types can virtually be extended by custom methods. If you specify a static method in a static class, which has the first parameter marked with the this keyword, the type of the parameter will be extended with the method.

s = "Hello World";
s.WriteToConsole();
var s2 = s.Concatenate("!");
...
public static class ExtensionClass {
public static void WriteToConsole(this string st) {
Console.WriteLine(st);
}
public static string Concatenate(this string st, string st2){
return st + st2;
}
}

The this marked parameter specifies the type which should be extended and is hidden when calling the method throught the type. All other params can be used to pass additional data to the extension method.

  • Lambda Expressions:
    You probably know the FindAll(..) method of Generic-List, where a predicate delegate is passed. This delegate specifies the condition of the search.
    In .Net 2.0 you could use the following statement to get all Int32 greater than 8 out of a list, by using an anonymous method.

List<int> list2 = list.FindAll(

delegate (int param){ return param > 8;});

With Lambda Expressions this paradigm is advanced, so that you can specify the condition directly in a particular syntax:

var

list2 = list.FindAll(i => i > 8); // all i, where i > 8

  • Object Initializers:
    It's possible to initialize type directly at declaration. This can be done by either setting properties or adding elements into an IEnumerable list (like already with array).

var list = new List<int> {1,2,3,4};
var point = new Point() {X=1, Y=1};

  • Anonymous Types:
    When executing database queries you get different columns back in a recordset. As developers we are generally lazy.. Why create different classes just for storing different database columns? Let the compiler do the work! By instantiating a new type without a type name, but using an object initializer the compiler will create a matching type for you on the fly.

var person = new {Name="Max", Age=23};
Console.Write("Name: {0} ({1})", person.Name, person.Age);

You'll be able to access person just like a regular object, but it will have a "unspeakable" name. You can only access it by using the var statement.

  • Automatic Properties:
    Same thing... hack for lazy devs.. How often have you specified a default property accessor for a field. Now you can do it by simply specifying it as followed:

public

string Name { get; set; }

This will be equivalent to:

private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

Besides there are also lots of other cool new features in C# 3.0 (Expression Trees, Partial Methods, ...).
If you want to play arount a little bit with C# 3.0 + LINQ just download the CTP here.

Comments

  • Anonymous
    November 13, 2006
    LINQ enables developers to query data sources directly out of code with a SQL like query language. You

  • Anonymous
    March 18, 2007
    I just got back from the Microsoft MVP summit in Redmond Washington. And while I had a great time, I

  • Anonymous
    August 09, 2007
    When using LINQ to SQL you need to create a DBML diagram! The first step to start with, is clicking "Add

  • Anonymous
    August 09, 2007
    When using LINQ to SQL you need to create a DBML diagram! The first step to start with, is clicking &quot;Add

  • Anonymous
    November 12, 2007
    Now it's getting confusing :-) As a prequel to my LINQ-To-Sql Article, I'd like to digg into the details