Why Should You Remove Unnecessary C# Using Directives

Why should you remove unnecessary C# using directives?

It won't change anything when your program runs. Everything that's needed is loaded on demand. So even if you have that using statement, unless you actually use a type in that namespace / assembly, the assembly that using statement is correlated to won't be loaded.

Mainly, it's just to clean up for personal preference.

Why remove unused using directives in C#?

There are a few reasons you'd want to take them out.

  • It's pointless. They add no value.
  • It's confusing. What is being used from that namespace?
  • If you don't, then you'll gradually accumulate pointless using statements as your code changes over time.
  • Static analysis is slower.
  • Code compilation is slower.

On the other hand, there aren't many reasons to leave them in. I suppose you save yourself the effort of having to delete them. But if you're that lazy, you've got bigger problems!

Preprocessor directives and unnecessary Using directives

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
}
}

How is performance affected by an unused using directive?

An unused using has no impact to the runtime performance of your application.

It can affect the performance of the IDE and the overall compilation phase. The reason why is that it creates an additional namespace in which name resolution must occur. However these tend to be minor and shouldn't have a noticeable impact on your IDE experience for most scenarios.

It can also affect the performance of evaluating expressions in the debugger for the same reasons.

Performance impact of unused using directives in C#

Extra Using directives will not have any memory/performance impact on final application - they are nothing but compiler provided shortcuts to deal with long type names. Compiler uses these namespaces to resolve unqualified (or partly qualified) type names to correct types.

Is there a performance gain in removing unnecessary namespace (using) directives?

No, there's no performance advantage.

The compiler doesn't generate IL (executable code) for using statements. IL is only generated for those classes, method calls, etc. that take advantage of the using statements you provide.

Consequently, the only effect unused using statements might have is to slightly increase your build time, or make the next developer after you wonder why they are there.

What are the benefits of maintaining a clean list of using directives in C#?

For me it's basically all about less noise (plus making Resharper happy!).

I would believe any improvement in compilation time would be minimal.

How to stop VS from removing unused namespaces if they are actually used in preprocessor directives?

Putting the using in a preprocessor directive seems to work.

#if !DEBUG
using xyz.EpiServer.Core.Tracking;
#endif


Related Topics



Leave a reply



Submit