5

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?

Paul Jansen
  • 1,116
  • 1
  • 11
  • 33

2 Answers2

14

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.

Lasse V. Karlsen
  • 366,661
  • 96
  • 610
  • 798
  • +1. (I've changed question to use "directive" instead of "statment") – Alexei Levenkov Jul 01 '14 at 14:41
  • Thanks Lasse. The 20 statements were just to make my point. The actual duplication is 14 statements. – Paul Jansen Jul 01 '14 at 14:45
  • 1
    Doesn't matter, to me it sounds like you should restructure the code, but this is for you to decide. The real answer here is that the C# compiler does not use a preprocessor, and does not handle include directives. As such, you're either going to have to reduce the number of using directives, or live with the duplication. To be honest, if that's the only thing wrong with these files I would leave it be. – Lasse V. Karlsen Jul 01 '14 at 14:46
1

Simple answer. If your code files are using 20 common using statements, they should not be separated into 20 statements. Also, I wouldn't worry that much over that kind of code duplication. It's not hitting your runtime performance in any way, so it's not really related to DRY paradigm.

Tarec
  • 3,207
  • 4
  • 28
  • 43
  • I wouldn't say DRY was related to improving runtime performance. It it more related to maintainability, reuse, and having the functionality only defined in a single place. In fact placing common code in a separate class/function is likely to make it slightly less performant as it has to be called from there rather than just compiled inline. – Sam Holder Jul 01 '14 at 14:41
  • DRY is most definitely not *only* linked to runtime performance. – Lasse V. Karlsen Jul 01 '14 at 14:42
  • In this case you're right, although IDE like VisualStudio is allowing you to hide whole `using` section and change the namespaces on-the-fly while refactoring, so I wouldn't really bother either. Of coruse it depends heavily on how complicated your project is so my advice may be wrong here. Still, it's a question worth upvoting. – Tarec Jul 01 '14 at 14:43