20

I am coding in VS2008 with Resharper 4.5.1, but the projects are set to target .NET Framework 2.0.

Still, Resharper is making suggestions that are relevant to the .NET 3.5 framework. For instance, it tells me that I should be using collection initializers, etc...

I've looked through the settings and can't seem to find the checkbox to tell it to give 2.0 specific advice.

bdukes
  • 152,002
  • 23
  • 148
  • 175
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • 4
    Collection initializers are a language feature, not part of the .NET framework. You can use certain bits of C# 3.0 in projects targeted at .NET 2.0. Does Resharper really suggest things that cannot be compiled? – dtb Sep 03 '09 at 17:32
  • Well, no, but it does suggest stuff that'll blow up on the target system, which only has .NET 2.0. – AngryHacker Sep 03 '09 at 17:41
  • Could you give more relevant examples then? – Jon Skeet Sep 03 '09 at 17:46
  • Well, the example I would think, would blow up on a .NET 2.0 only box, since the JIT compiler knows nothing about collection initializers. The answer provided by bdukes actually fixed the problem. – AngryHacker Sep 04 '09 at 04:46
  • 4
    Collection initializers are compiled into valid .net 2.0 IL by the C# 3.0 compiler. They will run fine on a box with only .net 2.0. The only C# 3.0 features that require .net 3.5 are extensions methods and LINQ, since they require types that are defined in new .net 3.5 assemblies. – bdukes Sep 08 '09 at 18:58

2 Answers2

43

Select your project in the Solution Explorer and open the Properties tool window (F4 in the standard keyboard layout or View > Properties Window after selecting the project). In the ReSharper section, there is a Language Level property that you can set to C# 2.0. Note that there are two separate project properties windows that manage different properties, if you see tabs for "Application", "Build" and "Debug" you are in the wrong window.

As others have said, this affects the version of C#, not the version of the framework (since most of the C# 3.0 changes can be compiled to an assembly that targets .NET 2.0).

thelem
  • 2,642
  • 1
  • 24
  • 34
bdukes
  • 152,002
  • 23
  • 148
  • 175
  • 1
    Can't find this option in Visual Studio 2010 with ReSharper 5.1 – altso Nov 30 '10 at 15:41
  • @altso Are you using C#? Do you have the project selected in the Solution Explorer? Are you able to see other properties for the project (e.g. "Always Start When Debugging" or "Project File")? Is there a *ReSharper* section in the properties? What kind of project is it? – bdukes Dec 01 '10 at 15:43
  • Ouch. I found it. Sorry for hesitation. – altso Dec 02 '10 at 15:38
  • 1
    Thanks for the suggestion @bdukes. Do you know if this can be set on a solution level? – Christo Jul 22 '15 at 21:46
  • @Christo I'm not aware of a solution-level setting for language version, but would definitely be interested in finding one (or a machine-wide setting) – bdukes Jul 23 '15 at 01:23
  • 3
    @Christo if you look in the `DotSettings` file that ReSharper creates, the language level (for C# 5) looks like `CSharp50`. You can move that from the project-level `DotSettings` to the solution level `DotSettings` and it appears to work (and, I assume, you could move it to any of the higher level files, as well) – bdukes Jul 23 '15 at 01:32
  • Please refer to http://stackoverflow.com/q/31574543/214747 for further details as to how i resolved my issue – Christo Jul 24 '15 at 08:42
  • 1
    @bdukes I can confirm it works at user level too. `%APPDATA%\JetBrains\Shared\vAny\GlobalSettingsStorage.DotSettings` – Nigel Touch May 03 '17 at 19:26
8

Those features are not .NET 3.5 framework features, but merely features of the 3.5 compiler. And since in VS2008 this is the compiler invoked for .NET 2 targets, it does handle these syntax extensions correctly.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • This is valid answer - OP is not using features he could used based on superstitions and misunderstanding of Compiler <-> Runtime separation. – kwesolowski Jul 24 '15 at 08:57