开发者

Using Statements vs Namespace path? C#

开发者 https://www.devze.com 2023-03-18 03:33 出处:网络
I recently stopped using using-statements and instead use the full namespace path of any .net object that I call.

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 decre开发者_如何学运维ase in this style of programming?


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(...) :)


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


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消