Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Does it matter how many using compiler directives are in my classes? Is there a performance gain in removing those that aren't necessary?

Although I enjoy writing streamlined code, on occasion, code segments get modified, and don't have the opportunity to go back and check to see if all of the included namespaces are really necessary. Or, I don't go back and remove those that are auto-inserted by visual studio.

i.e.:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

Thanks!

like image 308
Hoppe Avatar asked Aug 27 '11 22:08

Hoppe


People also ask

Do unused using statements affect performance?

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

Should using directives be inside or outside the namespace?

The using directive can appear: At the beginning of a source code file, before any namespace or type declarations.

Why remove unused usings?

Removing unused namespaces will reduce the number of autocompletion candidates in your text editor as you type.

Why remove unused using c#?

It also helps prevent false circular dependencies, assuming you are also able to remove some dll/project references from your project after removing the unused usings.


1 Answers

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.

like image 186
Robert Harvey Avatar answered Oct 12 '22 04:10

Robert Harvey