Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using directive vs full path name

Tags:

c#

.net

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 ?

like image 530
Jla Avatar asked Dec 03 '22 04:12

Jla


2 Answers

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();
like image 51
James Curran Avatar answered Dec 26 '22 09:12

James Curran


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.

like image 33
Matt Greer Avatar answered Dec 26 '22 08:12

Matt Greer