Refactor dotted namesapces in VS2010 Pro? - c#

How do I refactor the namespace of components that use dotted notation in my Visual Studio 2010 Pro solutions (C#)?
I have tried selecting Refactor->Rename or pressing F2 with the cursor at the head of the namespace string, but this only works for the first part of a dotted namespace string (e.g. "a.b.c.d" selects "a").
I've also tried highlighting the entire namespace and selecting Refactor->Rename but I'm presented with a dialog message that says: "Please select a symbol to be renamed."
Any help?

VS doesn't have anything built into it for this (yet).
ReSharper can do it for you:
For this i removed the .Entities from the namespace (sorry, i had to disguise some namespaces there, but you get the idea).
If you don't want to download a trial version of ReSharper and do it (hey, you may end up liking it!), then your best option is to ensure you have all your projects loaded, change the namespace at the top of the relevant class files, then attempt a recompile. All the faulty declarations or namespce useage will now show up in the Error List window - except for some XAML ones. (Some XAML useages will show up straight away, others will only show up at run time. You should be able to do a global search + replace on .xaml files only, this is reasonably safe, but make sure you have your files either backed up or managed in a source control repository).

Wouldn't find/replace do this for you? Search for namespace a.b.c.d, replace with namespace d.e.f; find using a.b.c.d, replace with using d.e.f.

Related

Searching and navigating through code in visual studio

I work on reality big project. And sometimes i get the need to search for some specific keyword inside A single c# file that has many calls of other functions from other c# file.
So i want to know if there is any easy way that could search for give phrase or keyword inside the current file and inside all the functions that my current file calls to. But not in the entire solution or inside the whole project.
For inside the file that's Ctrl+F.
Otherwise Code Search in VS2022 is very fast. Normally it's bound to Ctrl+T.
Introducing a New Way to Search Your Code and Visual Studio Features
Code search in Visual Studio 2022 is about to get much faster).
There also are the Go To options in the context menu that could help:
Finally, at the top of the editor window you can switch between classes and properties/methods.
BTW. You can address the large solution problem by splitting the codbase into multiple smaller solutions that only include parts of the overall code. This has some drawbacks, but overall it works very well in my experience.

How to add all missing usings at once with Visual Studio 2019 and C#

When I write C#, I sometimes have 3-4 missing usings which I can add with the tooltip window -> add missing using, or doing ctrl + . On the line where a missing using is present.
Doing this 3-4 times is rather annoying.
I have noticed a "add all missing usings" tooltip sometimes, which worked, but I can not get it to pop up reliably.
I know resharper has it, but I don't want to use a paid extension for this feature.
How can I reliably get this "add all missing usings" pop up, or in what other way can I add all missing usings at once?
Thanks!
The "add missing usings" is triggered by a paste action. After you paste new code, the option will be available so long as you haven't made any edits to the document after pasting the code (making any edit removes the option). Also, the option doesn't add missing usings for the rest of the document -it only analyzes the pasted section-.
You can cut out your code and then paste it to trigger the action as unfortunately currently it is not part of the default Quick Actions refactorings.
To complete #rhytonix answer...
after pasting your code, you have to click on the screwdriver in the left margin like this:
Also, have a look at the free Auto-Using for C#.
While I am not a huge fan of this, it might alleviate the pain of continually having to stop writing code to add a using statement. You can also view the project here on GitHub.
Personally, I just use Ctrl+. like you mentioned which fits in nicely with my way of writing code.

How do I automatically generate documentation/comments in Monodevelop?

Monodevelop automatically generates verbose documentation for functions and classes if "///" is typed in the appropriate place, upon typing the third '/'.
I want it to go over all of my code, though - I thought there was some button somewhere, and I looked around in the drop-down menus, tried right-clicking file names, etc.
I could not find such an option - does it not exist? I could not find a plugin that does that either.
Try to use Edit-> Document buffer.
It will make /// comments for all your public methods/classes without any comment upon it in current file.
It won't work if you have // comment upon method/class or for protected/private elements.
It is not possible. Your options are:
Manually typing three slashes before each and every function
Using an external tool
Online searches have not come up with any plugins for monodevelop that would do that, and the option does not exist in the vanilla IDE.

ASP.net c#, how do I know what to use?

I keep coming accross code samples online for ASP.net c#, however they never seem to list which namespaces they include, for example:
using System.Data.SqlClient;
etc etc
Am I missing something obvious or should I be expected to know exactly what namespaces each code example requires?
When I'm in that situation, typically I search for the class on MSDN. The documentation will tell you which namespaces contain the class.
If they don't include them, you can follow this list in order:
Find that they are in one of the namespaces listed in the "blank code file" template , or
In Visual Studio You can click the missing type and press shift+F10 or Ctrl+. To get the option to automatically add the using statement (if the assembly is referenced)
With Resharper, Select the type and hit alt+enter for Resharper to find the namespace for you, and add it to the usings (possibly even reference the assembly as well)
Go to MSDN and search the name.
Go to Google and search the name (honestly, I normally do this before hitting MSDN anyway)
Compain to the article author
If code samples use the assemblies that a project references by default, then you can hover on the class name and click shift+F10 which will add the using statement automatically. If the class is not in any of the referenced assemblies then you are out of luck and need to know in what assembly does the class resides.
A quick google search can help, and in time you will memorize the namespaces... Of course its best if samples included the namespace and reference info, but mostly they do not.
If you are viewing code in Visual studio, just hover mouse over class or object you want and you will get tool tip about it if assemly of that class is present or you can google for particular class.For example if you want to know more about 'DataTable'class, just google it and you will come to know that its part of Syste.Data namespace.
I'm with the OP on this one. Having to just magically "know" what namespaces are required seems supremely silly.
I spent some time before C# as a Java Developer, and the NetBeans IDE will resolve these for you automatically. Ctrl-Shift-I, and it will insert all the packages (ie, namespaces) you need. If more than one package defines the class you are resolving, a window pops up and lets you choose which one you want.
For as fine a product as VS is, I am incredulous that this feature is not included.

Refactor nested class - move to separate file - visual studio 2008

I am looking for a way to extract a nested inner class so that it is in a separate file. Is there an easy way to do this in Visual Studio 2008 (eg, using the refactor menu)?
EDIT
Maybe a refactoring menu option isn't necessary if manual refactoring is simple enough.
if i understand correctly, you can use partial classes
http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx
I don't remember there being a built in refactoring for that, but it is available in Resharper. If you don't have Resharper and have a lot of them you need to refactor, you could always download and use the trial.
DevExpress offers a free coding assistance tool called CodeRush Express which offers this refactoring. Here's a link to the page showing the refactoring.
I own a license to their full-blown CodeRush w/ Refactor Pro, and I love it.
EDIT
Following the edit of the question, I just wanted to add that I'm able to do that refactoring using the following keystrokes (with the cursor on the name of the nested class)
Ctrl+` (opens refactor menu)
Enter (or arrowing down to the option if it's not already selected)
That automatically creates a new file in the project with the name of the nested class as the name of the file, moves the nested class to that file, removes it from the original location, and drops a marker at the place where the nested class was removed. Hitting Esc takes you back to where the extracted class was removed.
It is a huge time-saver. Granted, my day's not full of extracting nested classes, but it's an example of how saving a couple of minutes here and there can really help remove the friction from some common development tasks.
This question is a bit outdated, but I found a very good solution since Visual Studio 2017:
If you have a nested class, place your cursor on the class name. A little screwdriver appears on the left side (alternative press CTRL + .).
In the context menu you can select "move to myClass.cs".
Now it will refactor your nested class in a seperate file with all namespace changes.
Here is how it looks:

Categories