开发者

Using directive vs full path name

开发者 https://www.devze.com 2023-01-10 03:59 出处:网络
Is there a general rule for referencing a type from a different namespace. Do you consistently use : using System.Drawing;

Is there a general rule for referencing a type from a different namespace. Do you consistently use :

using System.Drawing;

instead of defining it when needed:

System.Drawing.Graphics gr;
开发者_JAVA百科

I tend to type the full namespace when I'm only "calling" it once. Is there any best practice about that ?


What I use, (and what I believe most developers use), is to always say

using System.Drawing; 

and reserve

System.Drawing.Graphics gr;

only for places where there is a conflict with there being two Graphics defined in different namespaces, and, even then, I'll still rather use a using to differentiate them:

using MyGraphics=My.Own.Conflicting.Named.Graphics;
//  :
var myGraph = new MyGraphics();


Fully qualified names in code are distracting and noisy. I prefer to never have them. If there is a namespace conflict, a using alias can resolve that.

Besides, let's face it, 99.99% of us C# developers are using Visual Studio, so knowing what namespace and/or assembly a type is coming from is as quick as F12 or just hovering the mouse for a second.

The only time I like fully qualified names in code is in code samples, like here on Stack Overflow, to make it clear exactly what type is being used.


I only avoid using statements on conditions that using two different namespaces would introduce an naming collision.


Calling something once can turn into, further down the line, calling it 10, 20 or n times. My rule of thumb is that the C# design team put "using" into the language for a reason so I'll, if you'll excuse the pun, use it.

If, for example I was writing the following code:

private System.Drawing.Drawing2D.HatchBrush GetHatchBrush()
{
    System.Drawing.Color c = System.Drawing.Color.Red;
    System.Drawing.Drawing2D.HatchBrush h = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal, c);

    return h;
}

Which only uses Color and HatchBrush once, I'd certainly prefer to read:

private HatchBrush GetHatchBrush()
{
    Color c = Color.Red;
    HatchBrush h = new HatchBrush(HatchStyle.BackwardDiagonal, c);

    return h;
}

Even better, personally, would be:

private System.Drawing.Drawing2D.HatchBrush GetHatchBrush()
{
    var c = Color.Red;
    var h = new HatchBrush(HatchStyle.BackwardDiagonal, c);

    return h;
}

As James Curran has said, the only time I'd ever fully qualify a class with its full namespace, rather than using using would be when bringing a namesapce into scope would introduce ambiguity.


There is no generally accepted best practice here.
Just use common sense. The only goal here is readability.

When a relatively rare class is used just once or twice in your code, use the full name.

For most namespace, especially frequently used ones, put them in a using directive at the top.

And there are some very (very) rare cases where you need using to create aliases, but that has no real bearing on this issue.


I tend to type the full namespace when I'm only "calling" it once. Is there any best practice about that ?

Generally I see "using" being used even when only calling something once, this seems to be the standard (and is also what Resharper will push you towards, and many people tend to like their default conventions)


If you think in modules that should answer the question a little better for you.

Its a question of you will have name collisions and if you are abusing name space usage or not.

If you have a module dedicated for graphics say, and if that module is only used through its interfaces then you will not even access it outside that module. In this case you would be using Using (so to speak!) inside the module, and so it would be a good thing for comprehension and lets say it, your fingers too!

0

精彩评论

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

关注公众号