C# .NET 3.0/3.5 features in 2.0 using Visual Studio 2008
You can use any new C# 3.0 feature that is handled by the compiler by emitting 2.0-compatible IL and doesn't reference any of the new 3.5 assemblies:
- Lambdas (used as
Func<..>
, notExpression<Func<..>>
) - Extension methods (by declaring an empty
System.Runtime.CompilerServices.ExtensionAttribute
) - Automatic properties
- Object Initializers
- Collection Initializers
- LINQ to Objects (by implementing
IEnumerable<T>
extension methods, see LinqBridge)
Pretty much everything! Daniel Moth covers this here and here. That only leaves runtime support: LINQ-to-Objects is provided by LINQBridge - which leaves just bigger APIs like Expression support, and tools like LINQ-to-SQL. These are too big to be reasonably ported back to .NET 2.0, so I'd use .NET 3.5 for these.
I cover this in an article on my site.
Almost all C# 3.0 features are available when targeting .NET 2.0. For extension methods, you need to define an extra attribute. Expression trees aren't available at all. Query expression support is based on a translation followed by "normal" C# rules, so you'll need something to provide the Select, Where etc methods. LINQBridge is the de facto standard "LINQ to Objects in .NET 2.0" implementation. You may well want to declare the delegates in the Func
and Action
delegate families to make it easier to work with lambda expressions - and then remove them if/when you move to .NET 3.5