Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicated using directives in multiple files

I have got 5 C# files that have 20 using directives in common. I want to get rid of this code duplication, especially because these 20 using directives belong logically together. In C or C++ I would have created an extra header file containing these 20 include files. This extra header file acts as a layer then to include the 20 other files in one go.

Unfortunately I don't know how to do this in C#. Any suggestions?

like image 930
Paul Jansen Avatar asked Jul 01 '14 14:07

Paul Jansen


1 Answers

You can't do this in C# since C# does not have a preprocessor, and the C# compiler, although it supports some syntax that looks like preprocessor syntax, is handled by the compiler itself, and the compiler doesn't support include directives.

Also, if even one file actually requires 20 using statements it sounds to me as though you have put too much responsibility into one class. Perhaps this would be better served by refactoring and restructuring the codebase, thus reducing the number of required using directives instead?

Or, you could look at it from a different angle. You say that these 20 using directives all "belong logically together", why then 20 namespaces? Sounds to me as you've spread out some functionality into too many namespaces.

So either way I would look at restructuring the code.

like image 195
Lasse V. Karlsen Avatar answered Sep 24 '22 13:09

Lasse V. Karlsen