Search code examples
c#optimizationusing

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


Solution

  • 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.