Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do make unwanted namespaces to not appear by default on top of new classes

In Visual Studio 2008 C#, if I create a new class the following namespaces appear by default and I remove them manually every time. Is there a setting/folder template where I can go and remove these unwanted namespaces from appearing on each and every new class that's created on the project?

using System.Collections.Generic; using System.Linq; using System.Text;

like image 236
Enggr Avatar asked Apr 16 '10 14:04

Enggr


People also ask

How do I change the default namespace in C#?

You could put the caret on a namespace declaration in your C# code and press F2 on the keyboard. This should let you change the name of the namespace across the entire project, including the XAML files.

How do I delete unused namespaces in Visual Studio?

First is putting your cursor on one of the grayed out lines and clicking the light bulb, then clicking Remove Unnecessary Usings. (NOTE: The shortcut key for this is CTRL-.)

How do I change the default namespace in Visual Studio?

You can change the default namespace: -> Project -> XXX Properties... Instead of Find/Replace, you can also right click the namespace in code and Refactor->Rename.

What is default namespace in VB net?

VB.NET uses the name of your project (WindowsApplication1 for a standard forms application if you don't change it) as the default namespace.


2 Answers

This is coming from the ItemTemplate for a new class. Go to

[Program Files]\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033

(possibly a different LCID if you have a non English installation), and you can alter Class.cs inside Class.zip to fit your needs. Then clear out the cache at

[Program Files]\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplateCache

You should find your classes now get created in whatever way you just altered the template. Keep in mind this is not supported behavior, you are effectively "hacking" VS (although in a very trivial way)

The supported way to do this is to create your own template and use that, as shown here: http://www.switchonthecode.com/tutorials/visual-studio-how-to-create-item-templates

like image 135
Matt Greer Avatar answered Sep 23 '22 17:09

Matt Greer


To add to Matt's answer, you will find that depending on the project type you will see different sets of namespace imports. I suspect these are separate templates but some of the templates may be difficult or impossible to modify depending on how they were implemented. For example in a WPF or Silverlight application you get a whole bunch of System.Windows.* imported namespaces that you don't see in a normal class library project.

Another option would be to map a key sequence to the RemoveAndSortUsings command. If you go to Tools -> Options -> Keyboard you will see the keyboard shortcut interface. Just type "sort" and you should find the command, then map some key sequence to it and you can easily invoke that command whenever you want to tidy things up.

like image 26
Josh Avatar answered Sep 24 '22 17:09

Josh