Jens Bergvall
Jens Bergvall

Reputation: 1627

Preprocessor directives and unnecessary Using directives

I have some code that i need only to be run in a deployed environment (stage/test/production).

The code genereates some usings to other namespaces that appear at the top of the .cs file.

However, when i put this code inside my preprocessor directive #IF !DEBUG the usings at the top of the .cs file now claims

Using directive is unnecessary.

This feels dangerous. Since this is a multiple personnell project, i fear that someone is going to enter this file at some point and just auto-remove the usings since they're flagged as unnecessary.

My current solution is to just comment the code at the top of the file to not remove them.

So, how can i make my unnecessary usings directives non-unnecessary?

Upvotes: 1

Views: 439

Answers (1)

Murray Foxcroft
Murray Foxcroft

Reputation: 13755

Place your usings a #IF too:

#IF !DEBUG
using MySpecialStuff 
#endif

public class MyClass
{
    static void Main()
    {
#IF !DEBUG
        var msc = new MySpecialClass(); 
#endif
    }   
}

Or, fully qualify the class you need in the declaration.

public class MyClass
{
    static void Main()
    {
#IF !DEBUG
        var msc = new MySpecialStuff.MySpecialClass(); 
#endif
    }   
}

Upvotes: 3

Related Questions