Why remove unused using directives in C#?

后端 未结 10 1703
猫巷女王i
猫巷女王i 2020-11-30 20:10

I\'m wondering if there are any reasons (apart from tidying up source code) why developers use the \"Remove Unused Usings\" feature in Visual Studio 2008?

10条回答
  •  佛祖请我去吃肉
    2020-11-30 20:31

    Recently I got another reason why deleting unused imports is quite helpful and important.

    Imagine you have two assemblies, where one references the other (for now let´s call the first one A and the referenced B). Now when you have code in A that depends on B everything is fine. However at some stage in your development-process you notice that you actually don´t need that code any more but you leave the using-statement where it was. Now you not only have a meaningless using-directive but also an assembly-reference to B which is not used anywhere but in the obsolete directive. This firstly increases the amount of time needed for compiling A, as B has to be loaded also.

    So this is not only an issue on cleaner and easier to read code but also on maintaining assembly-references in production-code where not all of those referenced assemblies even exist.

    Finally in our exapmle we had to ship B and A together, although B is not used anywhere in A but in the using-section. This will massively affect the runtime-performance of A when loading the assembly.

提交回复
热议问题