tags:

views:

499

answers:

3

Are there any predefined constants in the C# compiler to detect what version of the platform is being targeted?

I can set a platform in Project options but how can I write single source for .NET 2 and .NET 3.5 ?

Edit: I really want to write some code once and be able to switch behavior with something like #if CLR_VERSION35 and then rn different configurations.

The reverse question: If I make CLR35 and CLR20 configurations is it possible to select target platform based on this? The option is not available in VS2008 and I don't know a lot about MSBUILD yet.

+1  A: 

In VS2008 you can set the target framework in the project properties (Solution explorer, right-click project). You also have the option when you create a new project, there's a drop-down in the top-right of the new project window.

Anthony
+4  A: 

.NET Framework 3.5 is a superset of 2.0 so all 2.0 apps should run on 3.5 without modification. As it is a superset there's no way of back-peddling to not use 3.5 functionality if your app finds itself on 2.0, not that makes sense anyway.

If you need to target 2.0 and you can write what you need in 2.0 then that's the way to go, 3.5 will still run your app fine. Effectively, you have to write for the lowest common denominator or include the 3.5 runtime redistributables with your installer to ensure that your customer has the correct environment for your app.

Lazarus
A: 

Aside from DEBUG and RELEASE there aren't as far as I know. I would save the searching and just define my own inside the project properties,"Build" tab, "Conditional compilation symbols".

There more information on this blog if that's any use

Chris S