0

My question is similar to this but I want to know if I can use different versions of C# in older versions of .NET. In my head it must be possible because for example, a Lambda has nothing to do per-se with the framework, it's a C# (and VB but I'm only talking about C# here).

The problem is, I can't test this as only have VS 2005 at work and my home PC has broken.

So, when creating a .NET 2.0 project in VS 2010/2012, can I use lambda's / Linq etc?

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Dave
  • 8,163
  • 11
  • 67
  • 103

4 Answers4

4

So, when creating a .NET 2.0 project in VS 2010/2012, can I use lamda's / Linq etc?

I have a page for exactly this sort of thing. Some features are framework-specific (or CLR-specific), and some aren't.

For example, you can use lambda expressions, anonymous types, and automatically implemented properties with .NET 2.0 with no problems.

You can use LINQ to Objects if you provide an alternative implementation such as LINQBridge. Other LINQ providers (LINQ to SQL etc) may apparently be possible using libraries from Mono, but personally I'd rather upgrade to a later version of the framework if at all possible.

You won't be able to use dynamic typing from C# 4, as it requires types which are only present in .NET 4, and which I don't think are easy to backport in a compatible manner. (It may be possible, as there at least was a version of the DLR for .NET 3.5, but whether that plays nicely with the C# 4 compiler is a different matter.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ha - the last few of my questions were based upon your examples of the Singleton pattern - I guess I should really buy the book as it seems to cover a lot of the ground I'm trying to understand. :) – Dave Oct 01 '12 at 08:51
  • The compiler allows me to create a dynamic type but not use it in any way! *var sb = new StringBuilder(); dynamic dsb = sb;* compiles without error, but I can't access any of the dsb methods! I guess this is (as per your comments) where I need .NET 4.0 – Dave Oct 01 '12 at 09:03
  • @DaveRook: Yes, I suspect so - I'm actually surprised it works even that much. – Jon Skeet Oct 01 '12 at 09:10
1

If you set your target framework to 2.0 and use Visual studio 2010, you can use Lambda expressions. But you can't use LINQ out of box, since the framework was introduced in .Net 3.5

You should see the article: Untangling the Versions by Jon Skeet

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    A blanket "you can't use LINQ" is a little bit strong, IMO. You can't use LINQ out of the box, but you can use *some* aspects of LINQ with a bit of work. – Jon Skeet Oct 01 '12 at 09:10
  • @JonSkeet, thanks, What I meant was out of box. I haven't used LINQBridge. I have modified my answer to reflect your comment – Habib Oct 01 '12 at 09:30
0

The language version and the framework versions are actually tied together, and Visual Studio will also determine what features are available depending on project settings.

For a .NET 2.0 project, you will not be able to use LINQ, as this will be tied to C# 2.0 and not C# 3.0 (which is when LINQ was introduced, as were extension methods).

Note that the 3.0 and 3.5 frameworks use the 2.0 runtime.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

There is the language and compiler, and there is the CLR. You can run .NET 2.0, 3.0 and 3.5 on CLR 2.0. You can not use .NET 4 functionality on CLR 2.0.

Thus, you can set your project to .NET 3.5, compile it on a machine with .NET 3.5 and run it on a machine with .NET 2.0.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • You'd want to target it to 2.0 though, in order to avoid using types or members which aren't present in 2.0. – Jon Skeet Oct 01 '12 at 08:50