I have completed day one of the Visual Studio "Orcas" and .NET Framework 3.5 Training Workshop. Today included a great overview of many of the new language features coming in this release. Unfortunately there wasn't enough time to go over all of the new features, but the features that I saw, I like.
Perhaps the defining feature of .Net 3.5 is LINQ (I haven't figured out the "official" way of pronouncing this yet, but I heard it pronounced as link at the workshop today). LINQ stands for Language INtegrated Query and is basically just that. LINQ allows for a developer to query any IEnumerable data source using a strongly typed, compiler validated, and Intellisense enabled syntax. If you are familiar with T-SQL, you will probably be comfortable with LINQ. The biggest difference between T-SQL and LINQ is that a LINQ query starts with the From keyword. There are also numerous other differences, however, given the Intellisense, they are easily discoverable (IMHO, one of the most important aspects of any API).
[UPDATE 06/26/2007]I forgot to mention a couple of things yesterday. According to one of the speakers, the primary performance goal of LINQ is to be as good as a for loop. However, there was a mention of a team working on a divide-and-conquer algorithm that will make it far faster on multi-proc/core machines. At this point it's just research. There is no date for release and there is a possibility that it won't get released. I should also have mentioned that there appear to be many different implementations of LINQ, there is LINQ for SQL (DLinq), LINQ for Entities (Entity Data Model), LINQ for XML (XLinq), and LINQ for Objects. There might be others as well. I'm not entirely sure of all of the terminology or how they are all used.[/UPDATE]
It appears that C# and VB.Net have widely differing capabilities in regards to LINQ. They both support a basic set of LINQ features, but VB.Net has a lot of keywords that are not available with C#. Many of these differences are based on the philosophy of each language team, but I imagine that developers will eventually demand (and hopefully get) features that one language supports that the other does not.
I thought the C++ section was going to be more interesting than it actually was (if you read my previous post about this conference you would know that I didn't think it was going to be that interesting :). I pretty much surfed the Internet the whole time and didn't get anything interesting out of it (sorry for anybody that is interested in the new improvements for C++).
However, I did pay rapt attention to the new language features for C# 3.0 and VB.Net 9.0. Most of the new features either directly support LINQ or are necessary to enable LINQ but can be used for other things as well. One of the cool things about the latter set of language enhancements is that they seem to be backwards compatible with .Net 2.0, so if you use Visual Studio 2008 to create .Net 2.0 applications using the built in ability to target different versions of the .Net framework, you can use some of the new language features!
Below I have included a list of some of the features that I found interesting. Unfortunately, given how quickly the speakers went over these features, I do not fully understand some of them, so if you find something interesting, you might want to look into it further just to make sure I'm not completely wrong about it ;).
New Language Features for C# 3.0
- var keyword - Declares a variables that will be created as the type that it is set to. This is particularly useful for LINQ, but it can also be used in foreach...
foreach(var cus in customers) // cus is strongly typed as a Customer type.
- Object initializers - Allows you to initialize an object by setting properties.
var cus = new Customer() { Name = "Boeing" };
- Collection initializers - Similar to object initializers, but used to load a collection. Any Add method exposed by the the collection can be used to define the initializer. This allows you to add elements to lists as well as dictionaries or any custom Add method that you create.
- Auto-implemented properties - Allows you to define a property without having to define the field or the get and/or set. The code is inferred by the property.
public string Name { get; set;} // this is expanded by the compiler to be a standard property with corresponding field
- Anonymous types - Allows LINQ to automatically create a type when transforming data. The type is not directly available from code, but you still get Intellisense to help you use the object.
- Lambda expressions (=> operator) - This is basically a lightweight version of anonymous methods. A lambda expression can be passed as a parameter to another method, just define a delegate that matches the lambda expression.
- Extension methods - Allows a type to "attach" methods to another type. To do this, add the this keyword in front of the first parameter in a static method.
public static string DoSomething(this string s) // This can be called like this - var something = "Hi".DoSomething()
- Partial Methods - I didn't quite catch how this works, but it seems like it allows designer generated code to define a method that you can implement.
New Language Features for VB.Net 9.0
- XML Literals - XML is now a first class feature within the language. You can can embed XML directly in the source code (without using quotes) and get Intellisense to boot (if you import your XML namespace). You can also embed ASP.Net-like code in the XML. This gets really interesting when combined with LINQ.
Dim myXml = <People><Person><%= person.FirstName %></Person></People> ' Example of using XML as a native feature in VB
- New XML API - The new API is much easier to use than the DOM, especially when creating a new XML document. (if you are looking for keywords to find it on Google, try XElement and/or XAttribute). An XElement is automatically created when using a XML literal.
- XML properties - .., .@, <> can be used like properties. This is a very cool way to get Intellisense support for XPath-like syntax.
- XML namespaces - XML namespaces can be imported similar to regular .Net namespace imports.
Imports <xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
- Anonymous types - Similar to C# anonymous types above.
- Into keyword - Calls LINQ aggregate methods, available when using the Group By (I do not believe this is available in Beta 1 of VS 2008)
- Group Join keyword - Another VB.Net LINQ feature that allows hierarchical results.
- Aggregate keyword - Alternative starting keyword to LINQ query that will return a scalar value instead of collection. The scalar value can be a complex type, but you will only get one of them. This is typically used with the aggregation methods such as Sum, Average, etc.