1
votes

I want to create a file where all my necessary namespaces will be, and use that file(, may be a superNameSpcae) in my code. To elaborate... I'm using few namespace like 'System; System.Collections.Generic; System.ComponentModel; System.Data; System.Diagnostics; System.Drawing; System.Linq; System.Text; System.Threading.Tasks' etc, in different class files. Writing all these for all class files are redundant. I want to put all those (namespaces) in a separate file, and call/use that in necessary class files.

How to do it?

2
Are you talking about creating a central place for your using directives?Damien_The_Unbeliever
Yes, I was thinking of creating a central place for my 'using.....' directivesraajputro

2 Answers

1
votes

Are you using Visual Studio? If so, you can change the template of a new C# code file to include all your namespaces when first created.

  1. Add a new C# class to a project and modify it to whatever you want (add default namespaces, etc.).

  2. You can do some parameter replacement like class name, package name, author, etc. through variables in the file such as $xyz. More details here.

  3. On the File menu, click Export Template.

  4. Follow the wizard to select a name and description for your new item template.

From now on, you can use this item template instead of the default "C# Class" template.

See How to: Create Item Templates for more details.

1
votes

You have two options, you can write a template for visual studio so when you create your class originally it automatically has the namespaces you commonly use or you can use code snippets to easily add your common namespaces to existing files.

There is no way to put the namespaces in a single separate file and have it reference them.