user1547769
user1547769

Reputation:

using directive with whole program scope

How can I have a using directive that has a scope of the whole program instead of just the file it is in. For every module that I use I would only want to set this once. If there is no way to do this what is the workaround to get this to work.

I thought about putting them all into a header file but C sharp does not have a file include.

Upvotes: 0

Views: 259

Answers (3)

L-Four
L-Four

Reputation: 13541

This is not possible. Fortunately.

Just wondering why you want to do this. This would be a bad programming habit anyway - put concerns and dependencies only when you really need them. If you add the same using directives everywhere, you also need the referenced assemblies everywhere, something you really don't want to do. Your project should rely on assemblies it needs, no more, no less.

Your argument about wasting time is a bit strange; if you loose so much time, you might have another problem in your architecture.

Note: Resharper is a tool that you may find useful.

Upvotes: 2

Vegard Larsen
Vegard Larsen

Reputation: 13047

Having the using directives in each single .cs file allows you to take that file out of an existing project and drop it into another project, provided it has the same references.

Also, adding using directives is basically automatic at this point. My tool of choice, ReSharper. Place the cursor on whatever class isn't recognized, press Alt+Enter, and the using directive gets added automatically. If there are multiple choices, you get to pick the right one.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Sorry, but what you are trying to achieve is not possible and I don't think that there are any workarounds. This is supported for example in WebForms or Razor views where you could declare common namespaces in the <namespaces> section of your web.config but unfortunately such artifact doesn't exist for standard C# files.

Upvotes: 0

Related Questions