tags:

views:

101

answers:

6

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;

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

+2  A: 

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.

Rob
+2  A: 

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

kbrimington
And even then I use an alias on one of the namespaces.
Matt Greer
+6  A: 

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();
James Curran
+1  A: 

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)

kekekela
+3  A: 

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.

Matt Greer
A: 

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!

Rui