Why remove unused using directives in C#?

后端 未结 10 1600
猫巷女王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:33

    At least in theory, if you were given a C# .cs file (or any single program source code file), you should be able to look at the code and create an environment that simulates everything it needs. With some compiling/parsing technique you may even create a tool to do it automatically. If this is done by you at least in mind, you can ensure you understand everything that code file says.

    Now consider, if you were given a .cs file with 1000 using directives which only 10 was actually used. Whenever you look at a symbol that is newly introduced in the code that references the outside world, you will have to go through those 1000 lines to figure out what it is. This is obviously slows down the above procedure. So if you can reduce them to 10, it will help!

    In my opinion, the C# using directive is very very weak, since you cannot specify single generic symbol without genericity being lost, and you cannot use using alias directive to use extension methods. This is not the case in other languages like Java, Python and Haskell, in those languages you are able to specify (almost) exactly what you want from the outside world. But event then, I will suggest to use using alias whenever possible.

提交回复
热议问题