How can Resharper be made aware of the framework version? - c#

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.

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).

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.

Related

Why are .NET target framework defines not set?

The Target Frameworks documentation page cites that certain defines are automatically set based on the project's target framework, which I am assuming would change based on the Target framework field in Properties > Application:
Or when selecting the Target framework while creating a new Visual C# project (New Project > Visual C# > Console App (.NET Framework))
However as far as I've tested, none of the defines specified in the documentation are actually defined.
For example, If I create a .NET Framework 3.5 project or manually set an existing project to target .NET Framework 3.5 in properties, NET35 is not defined.
I've taken every single define listed on the documentation page and added debug code that prints if any or all are defined, and none appear to be defined:
Sourcecode: https://gist.github.com/JohannesMP/ece8987fa18b2eaf830d7426c2256d6b
Am I making an incorrect assumption about when or how these defines should be set? What other portable way would I to conditionally compile code based on the target framework on a per-line basis?
I am running Visual Studio Community 2017 Version 15.8.4 with the default Microsoft .NET Framework Version 4.7.03056
No. Such predefined constants would only work if you work on the new SDK project format, and with multiple target frameworks (net45 and netstandard1.3 for example). Their existence is to help you perform conditional compilation so as to support multiple frameworks. My blog post shows a concrete example.
So you should create a new project that uses the new format, while .NET Framework Console Application template is not.
In console application doesn't work but with window application if you remove from reference: the Microsoft.CSharp and System.Net.http

In Which .Net Framework Version C# 7.2 is Available

I tried installing .net framework 4.7.2, still the target framework is empty in the project properties. The program I'm trying to run uses readonly struct, which is part C# 7.2.
Could you please guide me .Net Framework version has C# 7.2?
Language features are independent of .Net version (Framework/Core/Standard).
You will need to use a compiler that understands the newer language features. Assuming you're using Visual Studio 2017, you should update to latest version.
By default C# projects in Visual Studio use the latest major release of the language (which would be 7.0 as of this writing). To change this, open up the project's properties, select the Build page, click Advanced..., and under General set Langauge Version to "C# 7.2".
Per phuzi's comment mentioning this question, you might need to update to the latest Visual Studio in order to see this option.

How do I change the .NET framework bootstrapper package?

I have a C# project that I previously had targeting .NET 4.0, and now I want to target .NET 3.5, but I am getting this warning:
The version of the .NET Framework launch condition '.NET Framework 3.5' does not match the selected .NET Framework bootstrapper package. Update the .NET Framework launch condition to match the version of the .NET Framework selected in the Prerequisites Dialog Box.
But when I look under Publish->Prerequisites, .NET framework 3.5 SP1 is checked.
What do I need to do to get rid of this warning? There is no checkbox for .NET framework 3.5 without SP1, can I just not check any box?
I checked the launch condition, and the .NET framework launch condition version is already 3.5.
I found that I needed to right click on my Setup and Deployment project, hit properties, go to prerequisites, and uncheck .NET framework 4 and check .NET framework 3.5.
I had done that for all of the projects but for the setup and deployment project. I didn't realize it had its own prerequisites section.
It's likely coming from the installer project that you have within the solution. I'm guessing you do have one, as it's the only place I've seen Visual Studio talk about launch conditions.
Select it in Solution Explorer, and then at the top click the little icon with binoculars (Launch Condition Editor).
Under Launch Conditions, right click on the .NET Framework and open the properties, and then change the Version to .NET Framework 3.5.
I just got that error and i fixed it easily by double clicking the error message.
A window shows up. Then you can modify the properties of the different elements on the page by right clicking them -->> properties.
Just make sure all the items on this page have the desired framework version.
PS: on VS2010

How to disallow var keyword under .net 2.0 target?

I created new project in Visual Studio with target framework 2.0. But even if I left somewhere var keyword Visual Studio successfully compiles project. Is this the correct behavior as var is 3.0 feature? Is there any settings to prevent code with var to be compiled?
var is a feature of C# 3.0, but it doesn't require any framework features. In other words, it's absolutely fine to use within a project targeting .NET 2.0. The same is true of many other features - anonymous types, automatic properties, lambda expressions etc.
See my versions article for more information. (I need to update it for C# 4 at some point...)
If you want to restrict yourself to C# 2.0, you can specify the language version by clicking on "Advanced" in the Build tab of the project properties, IIRC. (It's definitely there somewhere, but I'd rather have a cup of coffee than check for the exact location right now.)
var is purely a compile-time feature, once the assembly is compiled, the compiler inserts the actual type and the fact that you had used var is "lost".
So a project that's targetting version 2.0 of the framework can still make use of the var feature, because it doesn't actually affect the outputted assembly in any way.
As long as your project will always be compiled with Visual Studio 2008 or newer, you're safe to use C# 3.0 features. The .NET 2.0 target only restricts what libraries you can use, not what language features.
Either do full text replace of var by 1var1 and manually replace them or use pre 3.0 versions of compiler.

Assembly Compatibility Question

I have a basic server control that I've written using .NET 3.5. If I give that dll to someone who is running a .NET 2.0 application, will it work?
Unless you are referencing any DLL's included in .NET 3.0 or 3.5 then yes it should work because the CLR versions are the same in .NET 2.0, 3.0, and 3.5. I believe there may have been minor changes to the 2.0 assemblies ("red bits") but I'll have to double check.
You can even just change the project properties in Visual Studio to target .NET 2.0 to be sure. If your app doesn't compile then it wouldn't have run on .NET 2.0 anyway.
Scott Hanselman has posted about .NET 2.0-3.5 differences.
The first thing you should do is try changing the Target Framework to 2.0 and see if your Server Control project still compiles (Right click on the project, choose Properties, then change the Target Framework to ".NET Framework 2.0" and choose "Yes" in the "Target Framework Change" dialog). If it still compiles you're off to a fairly good start. Now compile it, add it to a new web site and check to see if it works.
Most of the time you'll probably find that your control won't be using any 3.5 specific features, so once you've tried compiling it against 3.5 you'll be good to go!

Categories