I have just installed VS2010 in order to do some XNA development and I have noticed, coming from using Eclipse on a day to day basis, that one very useful feature appears to be missing. When I start typing a class name from another namespace, intellisense doesn't display it so I cannot autocomplete. I have to type in the full name, hover over the typed class name with my cursor, then click the menu that appears in order for the using directive to be added.
Is there a way to get a similar behaviour like in Eclipse where it shows all class names in the current project and automatically adds the import when you autocomplete?
you must use visual studio + resharper.
Resharper is the best addin for vs. It's a productivity booster! Although it's not free, It's worth it and there's also a free 30 days trial.
When you start working with resharper, you can't leave it.
Also, i recommend you to use stylecop too. It's an open soucee code rule validations and enforcement
Related
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
I recently organized the classes in my project into folders and would like to have the namespaces of the classes renamed according to their folders. I have tried to rename them using 'Rename' (F2) but the compiler throws errors at me (even after rebuilding the solution). What would be the best way to do this? As the title states, I'm using Visual C# 2010 Express.
EDIT: I'd like to know how to do this manually in case I need to modify namespaces beyond a month from now. Resharper looks very nice, but I couldn't justify buying it at this point. Also, why the downvotes to my question?
EDIT: Is there really nobody who can tell me how to do this? Or at least tell me it's more trouble than it's worth? Or a workaround of sorts? Anything at all?
You can always install ReSharper by JetBrains:
http://www.jetbrains.com/resharper/download/
They have a trial that works well for about a month, it can accomplish exactly what you want by right clicking anywhere and selecting "Adjust Namespaces", it will do the needed refactoring and you might find it to be a great tool to buy.
I am a recently converted VB developer to C#, but there is one thing thus far that I haven't been able to find. In VB when I setup a new project I can specify the namespaces used in the project and add them to the default imports for all classes (so everything automatically has them as if I added "Imports System.Data.OracleClient" to each class). With C# I've found that I'm always typing these in for each new class. Is there a way to setup defaults for projects so it at least appends those to every class file for me automatically?
No there is no way. C# does not support the concept of project level imports or project level namespaces.
The only thing you can do is alter the item template you are using (Class.cs or Code.cs) to have the namespaces you would like. These files are located under the following directory
%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\itemtemplatescache\CSharp\Code\1033
Under here you should see a Class.zip and Code.zip directory each with a .cs file under them. This is the template file used when you do an "Add New Item" operation in Visual Studio. You can change these to meet your needs and have the default namespaces you'd like.
A slightly easier solution though is adding a per-user code file for the particular project you'd like. Simply create a code file you want to be the template for your application and then place it in the following directory.
C:\Users\YourUserName\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
This file will now show up whenever you do a "Add New Item" operation.
Others have suggested using templates etc. Personally I find it's just not a problem - I type the name of the class that I want to use into Visual Studio, and even if it's not found the "smart tag" (or whatever it's called) icon pops up. I hit Ctrl-. and it adds a using directive for me.
I think ReSharper helps to make this work even better, but it's so automatic for me now that I don't really think about it much any more. (I suspect the difference is that with ReSharper I can hit Alt-Enter at any point in the line and it'll offer the correction, instead of having to have the cursor in the type name itself for Visual Studio.)
With C# 10 this answer has changed.
C# 10 introduces [Global using directives][1]:
Global using directives
You can add the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation. This is typically all source files in a project.
no, there's no my namespaces in C#. I think you can probably accomplish the same thing with project templates or code snippets.
See this post for the answer..
Which, in a nutshell, is adding the usings you want to a template.
I believe you want to start here.
I'm doing some architectural cleanup that involves moving a bunch of classes into different projects and/or namespaces. Currently I'm moving the files by hand, building, and then manually adding using Foo statements as needed to resolve compilation errors. Anyone know of a smarter way of doing this? (We're a CodeRush and Refactor! shop, but I'd be interested to hear if Resharper has support for this)
Visual Studio 2019 provides at least 2 built-in options:
'Move to namespace...' refactoring can be triggered on any class, and VS will prompt for the target namespace.
'Change namespace to...' refactoring is provided for when the current file namespace doesn't match with the folder structure.
This can be used to move individual classes to a different namespace by:
creating the desired folder structure
moving the file
applying the mentioned refactoring (CTRL+. with the cursor over the namespace)
These operation ensures that all references are updated accordingly.
Visual Studio 2010 has the possibility to rename a namespace. Place the cursor over the namespace name and press F2. Or simply rename it in the code and press Shift+Alt+F10, Enter after seeing the red squiggle appear.
Reharper can also rename namespaces. Quote:
The Rename Namespace refactoring
allows users to rename a specific
namespace and automatically correct
all references to the namespace in the
code. The following usages are
renamed:
Namespace statements
Using directives
Qualified names of types
As mentioned in the comments, this answer is now outdated. Please see the up-to-date answer below
Resharper is the only tool I am aware of what has this ability. There is also a lot of other functionality that it has that is missing in CodeRush and Refactor!
This answer applies to at least Visual Studio 2013 and 2015 with no resharper required
Move class files to new folder
Open 'Find and replace'
Select 'Replace in Files'
Type the original namespace definition in the 'Find what' field eg. MyCorp.AppStuff.Api
Type the new namespace definition in the 'Replace with' field eg. MyCorp.AppStuff.Api.Extensions
Select the new folder using the 'Look in' field's browse button ..., or type the folder path
Press the Replace All button
There are partial solutions for VS 2015 & VS 2017 without Resharper using free extensions.
One extension which I like today (end of 2017) is the Fix Namespace VS Extension:
https://marketplace.visualstudio.com/items?itemName=vs-publisher-599079.FixNamespace#overview
It analyses the folder structure of your solution and offers namespace refactoring using that. Unfortunately it isn't perfect: It doesn't track dependencies that well, but solved the lion's share of the work for me.
With Resharper: CTRL+R+O
Then press the down arrow key twice to select Move Type To Another Namespace.
Since the answer above was provided (I'm guessing) this feature has been added to CodeRush. Just place the carat on the Type to be moved and you'll see a Move Type to Namespace option on the Refactor! context menu. This will move the type to the new namespace and update references. You may still want to move the file to a solution folder that matches the name of the namespace though.
It's not the best outcome but can be done without plugins or tools, only with Visual Studio. Find and replace in Entire Solution, Match case, Match whole word.
Find what: class name, Replace with: New.Namespace.ClassName (fully qualified class name).
If you have 100+ references of the moved class and other classes in old namespace what are not moved this is the only foolproof and free solution I found. The only case when it leads to errors is when you have same class name in other namespace.
If you cannot, or do not want to use Re$harper, Notepad++ is your friend:
Make sure you don't have usaved changes inside Visual Studio for the files you need to move to the new namespace
Open all the files that contain the namespace that needs to be changed in Notepad++
Open Find & Replace (CTRL + H)
Fill the Find what and Replace with fields
Press Replace All in All Opened Documents
Save all changes in all documents (CTRL + SHIFT + S)
Switch to Visual Studio and reload all the documents (Yes to all at the prompt)
DONE
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.