I've just listened to Ander's PDC presentation on C# 2.0 (TLS320). C# is incredibly cool, the new features are great and it's amazingly useful to have Anders explain them. Languages enhancements are as exciting for a developer as a better quality hammer is to a carpenter.
What's interesting about language enhancements is that to take advantage of them you have to teach your brain to think more like a compiler. The Java language exam is probably the best example of this that I've come across. Initially I thought that having to answer questions that the compiler would check was a waste of time. Luckily a Java development mate of mine argued that it was important, especially in becoming a better developer. Having done the exam, I have to agree with my mate that there are real benefits from being able to look at code and know how the compiler would treat it.
So listening to Anders it isn't just exciting to know that they've developed a compiler that knows how to compile impressive statements, but also realising that I'll have to train my head to do the same thing. Luckily I find learning things like this a real pleasure. Having new C# syntax to learn and play with is like a bright blue sunny sky for me after the grey clouds of my current project (arguing with a developer who stubbornly insisted that GOTOs were the best way to code a particular method). Sure this looks a bit scary now, but I can't wait to get familiar with the syntax and to start developing with this extra power:
class Dictionary: IDictionary
where K: IComparable
where V: IKeyProvider, IPersistable, new()
{
public void Add(K key, V value)
{ … }
}
Some random points from Anders speech:
- Generics These are great in general and I can't wait to be able to use them in anger. I have a brilliant C++ developer mate who said he wont switch to another language unless he can use collections without having to cast. Hopefully he'll be swayed. I also like the use of the T.default and null checking that is thrown away by the JITer if it's a value type.
- Anonymous methods Anonymous methods seem similar to Java's anonymous classes. Dare says: "[Anonymous methods] fix the lack of anonymous inner classes in C# by adding anonymous methods for use in the exact same situations most people use anonymous inner classes for". It was good to understand how the implementation works under the covers to allow anonymous methods to access the parameters of the outer methods (Steve Maine waxed lyrical about this feature and lambda methods on the bus back from Universal Studios, now I finally understand his passion). I love the idea of passing code as a parameter.
- Iterators I did once try and implement these in VB6 and it was a horror, so I'm glad the story is that writing iterators that can be consumed by the foreach statement is now easy. Also, statements like foreach (Item x in Items.Subrange(10, 20)) are just cool.
- Partial types Anders explained there were two main reasons for this feature. I was wondering since Juval Lowry couldn't really see much point in them when he did the .NET radio show pre-PDC. Basically they help people who generate code - the generated class can be regenerated without breaking code written in the other part of the partial class, and where there are long classes (though this is a bad idea in general). It's also a key part of the XAML story.
- Other enhancements Finally C# lets us have different accessibility levels on property set's and get's, so the set can be internal and the get is public. This just feels more right.