user448374
user448374

Reputation:

Unused using statements

I may already know the answer to this question, but I thought it was worth asking anyway. If I have a load of using statements within my code file that aren't being used;

  1. Does that have any sort of detrimental performance impact?
  2. How does the compiler deal with them at compile/run time?

Thanks

Upvotes: 28

Views: 6542

Answers (6)

Dyppl
Dyppl

Reputation: 12381

No performance impact in run time. However, a lot of namespaces can somewhat slow down your code completion and decrease productivity

Upvotes: 2

CodesInChaos
CodesInChaos

Reputation: 108880

It doesn't affect performance at runtime at all.

It will probably increase compile-time very slightly since:

1) The compiler needs to parse a few more characters
2) It has to look up identifiers among more candidates. But since this probably uses hashtables it shouldn't be expensive either.

I'd guess the compiler slowdown is negligible.

I'd guess it slows Intellisense down a bit more since the list it has to display and filter gets quite a bit longer.

Removing unused usings is more a stylistic than a performance improvement. And you need to be careful about extension methods since they are brought into scope by using statements. For example I don't remove using System.Linq even when it's currently unused.

Upvotes: 9

Alexander Rafferty
Alexander Rafferty

Reputation: 6233

It may make the compile time longer, but it will have either no or a negligible effect on run-time performance.

Upvotes: 0

Jazza
Jazza

Reputation: 1082

It has no impact at all - see response to this question:

How is performance affected by an unused using directive?

Upvotes: 0

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

There is no performance hit at runtime, usings are resolved at compile time and useless ones are ignored.

Upvotes: 1

Tim Robinson
Tim Robinson

Reputation: 54764

does that have any sort of detrimental performance impact?

No.

How does the compiler deal with them at compile/run time?

At compile time they do what you'd expect, i.e. act as namespace imports. They don't appear in the compiled binary: any reference to a type uses the fully-qualified name.

The compiler does check the namespace in a using statement even if it's not needed; if you remove a namespace, then all the using statements that refer to it will fail.

To me, redundant using statements are like redundant comments. They make no difference to the compiler, but include too many of them, and you risk confusing your developers.

Upvotes: 25

Related Questions