Tech-Ed - C# 3.0: Future Directions in Language Innovation from Anders Hejlsberg
The shape if things to come. That's a brief description of what Anders Hejlsberg just showed is in this first session today. Anders Hejlsberg is the chief designer of the C# language and gave a great presentation on the new features that await us in C# 3.0.
The design goals for C# 3.0 where:
- Integrate objects, relational data and XML
- Increase conciseness of the language
- Add functional programming constructs
- Don't tie the language to an API. (Language Integrated Queries, or LINQ does not map to SQL directly for example. Which allowed them to make the query constructs transparent for all versions of LINQ)
- Be 100% backwards compatible. C# 1.0 and C# 2.0 code will just compile and work without the need for conversions.
The most impressive new feature of C# is of course LINQ which has been talked about on several blogs and sites already. But LINQ is only possible because of the following new features in C# 3.0:
- Inference and Anonymous types. You ma have seen the var type already. Do not mistake this for Object (or VB.Net variant). It will actually be of the type that is assigned on the right hand side. For example: var number = 10; will make sure that number is an integer.
- Extension methods. We all write static methods to do stuff on objects. In C# 3.0, by adding this to an argument in a static method, the method becomes available as a method on an object. For example: public static JanInBarcelona(this string Jan) will make the method JanInBarcelona available as a method of the string object. So you could do this: "MyString".JanInBarcelona("Blah");
- Lambda expressions. I found this hard to explain myself, so just check-out this link. on Developer.Net.
- Object initializers. If you initialize a class, you can now set values to it's properties at that time. For example, if you have a class Point that has the properties x and y, you can now do this: Point a = new Point() { x=0, y=1};
- Collection Initializers. The same is true for collections. The rule here is that any collection that implements IEnumerable and provides an add method can be initialized, as in this example: List<int> num = new List<int> {1, 100, 233}; And when Add provides more than one argument, that will obviously work too.
- Automatic properties. When you want to create properties on a class, you currently need to define private variables and property get and set stuff. In C# 3.0, the following will create a property: public string Name { get; set;}. The compiler will generate all the underlying code that you would normally write yourself. If you need a read-only property, just add declare private set;. You can still assign the value for the property when you create the class.
A great way to see all of this, and more, in action is to download the CTP from the Linq project site on MSDN. In that download you will find the Linq Sample Query Explorer, with about 400 samples that use LINQ in combination with SQL, collections and XML.