How to configure namespace adjustments by ReSharper? - c#

I'm having a problem with ReSharper refactoring I just can't find a solution to.
Whenever I'm trying to adjust a name space through refactoring, when it says "Move to 'correct.namespace' namespace", ReSharper forcefully removes unused "using" statements from all the files it fixes the namespace in and it also opens all the files.
My question is, how to prevent ReSharper from opening all the files it modifies and also how to stop it from removing unused usings?
I don't want it to open 20+ files, neither do I want it to remove the standard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Please help.

Add them to the Namespace Imports part of the Resharper Options to preserve the namespaces.
I'm not sure if its possible to prevent it from opening all of the files since it depends on so much of the VS infrastructure. Plus it would not be possible to undo without opening the files.

Related

How to add using System.Reflection to all existing files [duplicate]

I am using resharper to do a big refactoring and i keep going from file to file and adding the same namespace over and over again in the "using" section
is there anyway to add a "using" statement to every single file in a folder, namespace or project? Even though some files wont need the reference, most do, so it will save lots of time.
I'd try a regex in the "Find and Replace" dialog:
Replace
^using System;$
with
using System;\nusing xxx;
This works only for files using the System namespace, but maybe you find another common namespace or structure element. After doing so you can refactor all files in your solution(/folder) with the resharper. That will remove doubled usings.
Update: Did you introduce new namespaces for existing types? There is a refactor function called "move". It will move your type to a new namespace and preserve the references.
Open ReSharper Options / Languages / C# / Namespace Imports
Add "Namespaces that should always be imported"
Run Code Cleanup against solution or project. You may want to create profile (Options / Tools / Code Cleanup) with only Optimize Using Directives module.
VS will add them for you. When you add a symbol in a referenced assembly, but without a using statement for the symbol, you will get a marker against the symbol. Press control-period (or use the mouse) and the first option will add the using statement for you.
Otherwise you could write a VS macro to open each project source file in turn and insert the statement.
I feel it's worth adding an answer to this old question, now that we have "global using directives" in C#10. It renders this requirement moot for newer codebases targeting C#10 and above.
We can now just put one global using in one file, for it to be accessible everywhere in the solution.
global using yournamespace
See the documentation here https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10#global-using-directives
When you encounter a file, one-by-one, press CTRL+ALT+SHIFT+F for the automated file cleanup routine. It just takes a second to run, and will do what you're looking for, but not just for System.
not sure if R# has a way to do solution wide file cleanups.

Using Directives Sorted in Wrong Order

I'm using the Power Commands extension with Visual Studio 2012. I have the option checked to remove and sort usings on save. The problem is that the System.Xxx directives are being sorted last, and that's causing a style analysis error:
SA1208: System using directives must be placed before all other using directives.
Before save:
using System;
using System.Diagnostics.CodeAnalysis;
using Foo;
After save:
using Foo;
using System;
using System.Diagnostics.CodeAnalysis;
This worked correctly (System.Xxx first) with VS 2010. Anyone know how to correct this?
Note: Even if it didn't cause an SA error, I'd still prefer the system directives to be first.
Goto the "Quick Launch" (Ctrl+Q) and type "using" and press Enter.
Then change the following setting:
It's an annoying default setting, I have no idea why Microsoft chose that, it goes against all previous standards that I've ever seen.
EDIT:
Thanks to Oskar we have a reason:
The reason for the change in default behavior is due to the fact that
Windows App Store applications prefer to have 'Windows.' at the top
of the file rather than 'System.'

VS 2010 keep stating that "This using directive is not required"

The following using statements in a regular class keeps screaming at me that "The using directive is not required".
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
Yet if I try to use the DateTime data type intellisense will not pick it up and the compiler complains when I try to build.
You could organize them:
Then when you want to use a new type in your code which is not in your usings declarations simply place the cursor over it and Ctrl+Alt+F10+Enter
This being said, organizing and removing unused usings is not compulsory. This has already been discussed here. It has strictly no difference at runtime nor at the emitted assembly. You should do it only if you are anal about how your source code looks like and have things properly organized. I do it all the time.
It ended up being a misunderstanding of how VS 2010 works. The IDE tells you that you're not using a library until you actually type the code that requires the library.
I thought the errors were being generated after I build the solution. The "organize usings" menu item in the context menu helps clean this issue up quick. Thanks for all your feedback.
Vs is telling you that you have added some libraries that are no required. remove the not used libraries and make a clean solution.
I think you are getting this error for using System; statement. Try removing that statement.

Why has my solution stopped recognising Using Directives?

I have a solution, which I was working on yesterday without issue. Today I have opened the same solution and VS 2008 has underlined certain using directives in red. Mousing over the underlined directives brings up the tooltip: "Am I missing an assembly reference?" I am not, as I tried to add the references in and it does nothing to solve the problem.
Weirder still the project compiles and runs without a problem so it's only VS2008 that's having a problem with the project. I'd like to get all my objects back to a state where Intellisense recognises them, trying to maintain the code when VS doesn't recognise objects like DataTable, RegEx and TableCell is very frustrating.
The using directives at the head of my page are as follows, the bold ones are no longer recognised after the System namespace reference, the others work fine:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
I don't really understand what's changed overnight to break the project. Any suggestions?
Some things you could try:
Delete the .suo and .csproj.user files in the solution and project directories, respectively. (The .suo file has the hidden attribute set.)
Create a new, blank project which doesn’t exhibit the problem you are experiencing, and then compare (using a file-compare tool) the new .csproj file with the .csproj that exhibits the problem. Remove things that appear to be redundant.
One thing that immediately jumps out is that all of the using directives that are not working come from assemblies other than the core .NET assembly. I would double-check that the references to these assemblies are still valid within your project file.
As some comments under my question recommended (and completely against the instincts of common sense) cleaning the project, rebuilding it and then restarting VS2008 did the job... Who can say why?
The only difference in my PCs start up this morning was that the company approved security solution McAfee System Hog (or whatever it's called) decided to soak up most of my system's resources downloading and installing some sort of update. During this period my PC was super slow to respond to anything I told it to do. Maybe the McAfee install interfered with the usual VS startup sequence.
Anyway, works now but I shall bear in mind other suggested solutions in case it ever happens again. Thanks!

Class file in visual studio 2008

seWhen i creating a new class file i got these namespaces by default,
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
But i dont use linq,Html controls,Webcontrols,Configuration,security...
Why are they included by default?
What will happen if i exclude these from my class file?
What will happen if i include these without using them?
Why are they included by default?
Visual Studio adds a list of common includes to any class file that you create. Since it is an ASP.Net project, this is the list of using statement added to your file. For WinForms projects, it is a different set of usings.
What will happen if i exclude these from my class file?
If you don't use any classes within these namespaces within your file, excluding them will have no effect. If you use a class within those namespaces, you will have a compile error.
What will happen if i include them without using them?
Maybe it will take a few milliseconds longer to compile that file but I'm not even sure.
These namespaces are included by default in files that you add to an ASP.Net project.
The using statement simply tells the compiler which namespaces the classes you're using are in.
If you aren't using any classes from those namespaces, the using statement will no effect whatsoever; removing them or keeping them will not matter.
Caveat: If there are two classes with the same name in two different namespaces (eg, System.Windows.Forms.Control and System.Web.UI.Control), and you having using statements for both namespaces, you won't be able to use the class unless you fully qualify it with the namespace name. (Because the compiler cannot know which one you want)
Nothing will happen if you exclude them.
Nothing will really happen if you include them.
It's recommended to remove the ones you don't need.
Not sure about the Web stuff, but Linq is included by default for all 3.5 applications, probably because it's assumed that you'll use them as much as you do system.

Categories