Using Statements vs Namespace path? C#

本秂侑毒 提交于 2019-11-26 12:47:05

问题


I recently stopped using using-statements and instead use the full namespace path of any .net object that I call.

Example:

using System;    

namespace QuizViewer
{
    class Class1
    {
        Console.WriteLine(\"Hello World!\");
    }
}

This is what I do now.

namespace QuizViewer
{
    class Class1
    {
        System.Console.WriteLine(\"Hello World!\");
    }
}

Before you ask why I do this, I am using this style so that I can see exactly where my objects are coming from and it\'s easier when using the different Timer objects and other objects with similar names.

Is there any performance increase or decrease in this style of programming?


回答1:


There is zero performance difference because the compiler ALWAYS puts in the full name - using is only a hint for the compiler, the runtime doesn't know or support that.

However, once you memorize where the objects come from you will look at this as silly and verbose. There is just so much noise and people just know that Path is from System.IO, Console is in System and StringBuilder is in System.Text.

One downside of your approach: Without using, no extension methods outside of the current namespace. Have fun writing System.Linq.Enumerable.Where(inputSequence,...) instead of just inputSequence.Where(...) :)




回答2:


Short answer no: it is the same code that is compiled.




回答3:


I think that this style result in a programmer performance decrease :). I use the using statement and usually it is clear from code to which namespace the class belong. If not, press F12.
Just my 2c.




回答4:


There's no performance impact; it's mostly a stylistic choice. I find that using using statements reduces the clutter. Plus, with Intellisense, it's easy enough to see the fully qualified namespace.




回答5:


The only performance hit, is the hit you take to type it all out, and with you or others reading it.

Using statements are to help readability, not really for performance.




回答6:


If you disassemble both of this pieces of code and look at IL code, you'll find that compiler always references all the types by it's full names. That is absolutely identical ways to operating types.



来源:https://stackoverflow.com/questions/6628531/using-statements-vs-namespace-path-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!