4

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.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
H H
  • 263,252
  • 30
  • 330
  • 514

3 Answers3

5

.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
  • 41,906
  • 4
  • 43
  • 54
  • What would be the solution in case if we have .net 4.0 also. – Tassadaque Jul 09 '11 at 20:06
  • @Tassadaque, the same as Lazarus describes. (1) write to the lowest common denominator, or (2) include the redistributables you ned for the verison you need. Fortunately, Setup Projects make the include of the redistributable pretty easy. And if you _really_ need to know at compile time, then it is as Chris S says in his Answer: add Conditional Compilation Symbols yourself to tell you what your source code needs to know. – Jesse Chisholm Jun 25 '12 at 22:10
2

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
  • 720
  • 4
  • 10
1

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
  • 64,770
  • 52
  • 221
  • 239