Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

namespaces

c#

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?

like image 945
Scott Salyer Avatar asked Apr 25 '09 16:04

Scott Salyer


People also ask

Does hep C go away?

3. Sometimes, the infection goes away on its own. Acute hepatitis is C is a short-term illness that occurs within the first six months after being exposed to the virus. Like the human papillomavirus (HPV), early acute hepatitis C can clear on its own without treatment; this happens about 25% of the time.

What is || in C programming?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool .

What is hep C positive?

A positive test indicates that you have been infected at some stage. It doesn't necessarily mean you are currently infected, as you may have since cleared the virus from your body. The only way to tell if you are currently infected is to have a second blood test, called a PCR test.


2 Answers

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.

like image 83
JaredPar Avatar answered Oct 01 '22 18:10

JaredPar


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

like image 28
Jon Skeet Avatar answered Oct 01 '22 19:10

Jon Skeet