tags:

views:

869

answers:

4

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?

A: 

no, there's no my namespaces in C#. I think you can probably accomplish the same thing with project templates or code snippets.

Arnshea
+2  A: 

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.

JaredPar
+1  A: 

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.

jlembke
+4  A: 

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.)

Jon Skeet
Continuing this thought, one of my favourite Resharper features is that you can type the first couple of characters of a class, press SHIFT + ALT + Space and it will create an Intellisense window that includes all matching classes in the project or referenced dlls. When you select one it will add the using statement if required. (This command is Resharper.Resharper_CompleteCodeTypeName if you want to find it in your key bindings)
Martin Harris