views:

1255

answers:

7

What are some of the new features that can be used in .NET 2.0 that are specific to C# 3.0/3.5 after upgrading to Visual Studio 2008? Also, what are some of the features that aren't available?

Available

  • Lambdas
  • Extension methods (by declaring an empty System.Runtime.CompilerServices.ExtensionAttribute)
  • Automatic properties
  • Object initializers
  • Collection Initializers
  • LINQ to Objects (by implementing IEnumerable extension methods, see LinqBridge)

Not Available

  • Expression trees
  • WPF/Silverlight Libraries
+1  A: 

Lambdas & Extension methods are handled purely by the compiler and can be used with the .Net 2.0 framework.

James Curran
Lambdas as delegates, yes. But lambdas as Expressions rely on the Expression class(es) from .NET 3.5.
Marc Gravell
+3  A: 

To define extension methods, you'll need to supply the following class if you're targeting .NET 2.0:

namespace System.Runtime.CompilerServices {
  [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
    sealed class ExtensionAttribute : Attribute { }
}
Curt Hagenlocher
I also had to do this (found you suggesting this on another site) to be able to use Ninject 1.0 for 2.0 in a 3.5 SP1 project. Very wierd...
Ruben Bartelink
+16  A: 

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<..>, not Expression<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)
Lucas
+2  A: 

There was a previous discussion about something similar you may also want to read too:

http://stackoverflow.com/questions/140239/targeting-net-framework-35-using-net-20-runtime-caveats

Kev
+5  A: 

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.

Marc Gravell
+5  A: 

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

Jon Skeet
+2  A: 

You can use Mono's version of the System.Core which fully supports LINQ & Expression Trees. I compiled its source against .net 2.0, and now I can use it in my .net2.0 projects. This is great for projects that needs to be deployed on win2k, where .net3.5 is not available.

Omer Mor
do these expression trees require .Net 2.0 SP1?
Lucas
nope. plain old .NET 2.0. Works great even on win2k machines.
Omer Mor