Does C# Support Project-Wide Default Namespace Imports Like VB.NET?

Solution 1:

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.

Solution 2:

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

Solution 3:

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.

Solution 4:

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

Solution 5:

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.