Extension methods and .NET 2.0

Update: as Daniel Moth has pointed out in the comments, it is possible to use extension methods in Orcas projects targeting .NET 2.0 by introducing your own System.Runtime.CompilerServices.ExtensionAttribute class. See Daniel’s blog post for more details.

One of the neat things about Visual Studio 2008 is that you can target .NET 2.0, 3.0 and 3.5. I’m hoping to start using it professionally shortly after it’s released – assuming stability and performance aren’t an issue, of course. The great thing is that some of the C# 3 features don’t require any framework support at all (and none of them require CLR support, as far as I’m aware). So, I’ll be able to use anonymous types, local variable type inference, object initializers, collection initializers and automatic properties. Sure, there’s no LINQ, but I can take that hit.

But what about extension methods? As far as I can figure out, they only require one feature from the framework: an attribute to decorate a method to say that it’s an extension method. If we could provide that as a compile-time option, we could use extension methods in .NET 2.0 projects, assuming I haven’t forgotten something important.

Now, I know there’s a lot of controversy about whether extension methods are really a good idea in the first place – but I for one would welcome the opportunity to use them in .NET 2.0 projects. My wild guess is that I won’t be able to use .NET 3.5 until mid-2009, and that a lot of other developers will be in the same boat. I’m sure the transition from 2.0 to 3.5 will be faster than 1.1 to 2.0 – and I doubt that many people will target 3.0 at all – but it’ll still be far from instantaneous.

The obvious downside of this would be everyone creating their own attributes and using them throughout their projects. However, it wouldn’t take long to remove them when the project moved on to .NET 3.5 – delete the attribute and remove the compiler option, then fix the compilation errors.

It’s too late to suggest this for the Orcas timeframe, really, which is a shame. I wish I’d thought of it earlier – although I suspect it’s been suggested before, so it probably wouldn’t make any odds. Anyway, what do you all think?

Is C# 3 too big to learn from scratch?

I’ve been looking at C# 3 in a fair amount of detail recently, and likewise going over the features of C# 2. (I hope to be able to be less coy about all this soon.) I’m beginning to think that while it’s all great for existing C# 1 and C# 2 developers, I feel sorry for someone wanting to learn C# 3 from scratch. It’s becoming quite a big language – and of course the framework is big and getting bigger (more on that in another post).

I’m generally a fan of small languages whose functionality is provided by libraries. This is still the case with C#, but the compiler is now being smarter in various ways to allow for a lot of the neat features in C# 3.

It’s often been said in the newsgroups (usually when someone has been moving from another language to C#) that C# itself only takes a few days to learn, but the framework takes a lot longer. The “framework takes longer” part is still true, but what about learning C# in a few days? I’m sure it depends on previous experience: someone coming from a functional background is likely to find the C# 2/3 changes to do with anonymous methods and lambda expressions significantly easier than someone moving from C or Java.

The interesting thing about the new features in C# 3 is that aside from query expressions, they’re really fairly easy to describe. There’s a big difference between reading the description of something and really “getting” the feature – and I’m not even talking about best practices and applicability, just sheer “understanding what’s going on when you see it in use”. Generics in C# 2 work almost the other way round – they’re quite complicated to describe in detail, but you can largely get on with using them and deal with details later. You end up with surprises such as the lack of type parameter covariance, but there will always be new things to learn with virtually any feature.

So, if you were learning C# from scratch, would you be daunted? As a rough indicator (and one which genuinely doesn’t have much to do with my writing at the moment) how big a book do you think it would take to learn C# (without Windows Forms, etc – just enough of the core libraries to understand iterator blocks, IDisposable etc)? I suspect it would be hard to do it any sort of justice in less than about 700 pages, which is a pretty off-putting size (at least for me).

I’m not sure whether it’s useful for an incoming professional developer to start off learning C# 1, then learn 2 when he’s comfortable with 1, and then 3 afterwards. It would be quite hard to be productive either working on new code or maintaining old code without being able to understand the syntax that colleagues are using.

Maybe there are enough existing C# developers and enthusiastic newbies who are willing to make such a significant commitment. Maybe I’m completely wrong about how hard it is – let’s face it, it’s always hard to gauge the difficulty involved in learning something after you already know it. I am concerned though. How does everyone else feel?