Removing redundant using statements - c#

Does anyone know of a tool to remove redundant using statements from classes, or a whole solution?
I'm using the Refactor! addin which has a "move type to separate file" smart tag, but it takes all the using clauses from the original class with it.

VisualStudio 2008 does this out of the box.
Simply right click in the code window -> Organise Usings -> Remove Unused Usings.
You can set up a shortcut key to do this, as explained here.

Resharper does this pretty well.

PowerCommands for Visual Studio upgrades the default VS.NET 2008 functionality of "Remove Usings" to an entire project or solution.
I use it all the time. It also has a lot of other useful features- check it out.
Best of all it is FREE.

ReSharper does this.

Related

Intellisense for available 'using/import's in C# with Visual Studio Code

Edit: Vidual Studio Code and Visual Studio are 2 different things. Yes it's confusing but I know that VS has this feature, I'm asking about VS code.
Is there some extension/setting that makes Visual Studio Code's c# have Intellisense for all available namespaces, including those that were not yet imported, and then imports them when selected?
Example: Collections are not yet imported and I want to type IEnumerable and import it. Being the average programmer this is quite tedious and I might screw up the spelling or capitalization, and then have to press ctrl+.. I would like to just be able to type "ienu" and then IEnumerable would pop up and would be autocompleted and auto imported.
This feature exists for Typescript in VScode (thanks to an extension), and even for C# but in Visual Studio with the Resharper extension.
I have searched everywhere for this feature but it seems to me like it doesn't exist. It honestly feels insane to code without this.
If you use "C# extension" for VSCode (this https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)
you can try follow next steps:
Right click on C# extension
"Extension settings"
In opened "Settings" tab, add import in search area
Enable Checkbox
(Screenshot of this step)
Enjoy autoimport without "special" extension (Screenshot)
I just went ahead and made the extension:
https://marketplace.visualstudio.com/items?itemName=Fudge.auto-using#overview
Currently it supports only the base C# libraries but I can make it use additional nugget libraries as well if there is demand for that.
As of 24/11/2020 the official C# extension supports this feature, see other answer.
There are some sort of solution for your problem that might help if you write the complete key word and don't want to write the using system or any library you can just put cursor on your key word and press alt+enter it will automatically add the library .but if you are not using library and want to auto complete key word of that library it's possible with re-sharper. you can also use Ctrl+space if your visual studio is not giving you auto recommendation

C# - No Intellisense for SqlMethods (Linq/EntityFramework)

I've seen numerous examples for SqlMethods.Like and want to try it, but I can't get intellisense to recognize it. What do I need to reference or what "usings" statements to do I need to add.
I figured it out:
Add Reference System.Data.Linq, add code:
using System.Data.Linq.SqlClient
See you've already answered this but for future reference in visual studio if you cursor over the code and press ctrl+. visual studio will recommend you a namespace to use.
Hope this helps you in the future.

Using VS Code Snippets with Resharper

I am trying to use Code Contract's Code Snippets but since I turned Resharper back on it doesn't recognize them. On the other hand, it is recognizing some snippets I've implemented myself in the past.
Any ideia of what might be the problem? I'm specifically trying to use cr and ce, which I think, don't collide with any other snippets (at least from what I see in the intellisense).
I'm using R# 5 with VS 2010
Thanks
Press CTRL+J then type your snippet shortcut... this is mentioned here on the Resharper site
This answer demonstrates another work around.
Right-clicking in your code editor and selecting "Insert Snippet"
CTRL+K,X
This is what I am using to use mvvmlight snippets.
I have the same problem and would gladly see a solution. The snippets work even though they won't show up in the intellisense popup, so if you hit "cr"+tab you would probably get the snippet anyway.
There is a "workaround" though. If you go to open the ReSharper settings and Environment->IntelliSense->General and change from ReSharper to Visual Studio intellisense mode. This will make your snippets show as expected, however some of the resharper intellisense additions won't be available.
I've tried with the latest nightly build of ReSharper 5.1 and it's still not fixed, unfortunately.
Hope this helps.

Show method name in "Find all references" - C# Visual Studio

When i rightclick something in visual studio and press Find all references i get a list of all locations where this object occur.
Is there any way to also show the scope of this location (Namespace.Class.Method) instead of just a line-number. Now i have to doubleclick all of the results to see where it actually is.
One more vote for ReSharper. Check here for some screenshots as to the type of results. You can change how the results are presented (My favourite is Project+Namespace+Member, so I can see which methods the references are coming from.)
You also have a few options to refine your searches. For example:
Search project / file or even include referenced libraries
Search for Read or Write usages
Search for "text" matches
They have a free trial available... try it out.
Sorry, for this you'll need a productivity tool, like Reshaper. (Other productivity tools, such as DevExpress, do exist, however I don't know if they also provide such a feature.)
Using the default feature in Visual Studio, you can't achieve that. The Find All References seems to use the same search engine as the Find In File.
As M.Chohan said, a plugin like Reshaper or DevExpress CodeRush might have the feature you are seeking.

Refactor "using" directives over an entire codebase?

One of the things I love about Visual Studio 2008 is the ability to refactor and reorganize the "using" directives in source code files (this may have been in 2005 as well, I don't remember).
Specifically, I'm talking about how you can have it both reorganize the directives to be alphabetical (though with the core FCL libraries floating to the top) and removing any directives which don't need to be there (either never used or no longer used).
Is there any way to automate this refactoring (the sorting and trimming) across an entire old codebase? Either through all of the files in a solution or across multiple solution files.
I believe you can do it solution wide using Power Commands
From PowerCommands Documentation:
Remove and Sort Usings
This command removes and sort using statements for all classes given a project. It is useful, for example, in removing or organizing the using statements generated by a wizard. This command can be executed from a solution node or a single project node.
ReSharper is a (commercial) Visual Studio plugin that has a "Code Cleanup" utility than can be run at a solution-wide level. The utility detects and removes unreferenced using statements, and performs other useful maintenance. I am unsure if it provides functionality to sort them, however.
Use PowerCommands addon for Visual Studio '08. You can right click the solution in the Solution Explorer and choose "Remove And Sort" usings. Also, there are options in the Tools-->Options dialog box (scroll down to the Power Commands item) for removing on save and reformatting your code ( using vs settings ) on save.

Categories