There is actually a (subtle) difference between the two. Imagine you have the following code in File1.cs:
// File1.cs
using System;
namespace Outer.Inner
{
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now imagine that someone adds another file (File2.cs) to the project that looks like this:
// File2.cs
namespace Outer
{
class Math
{
}
}
The compiler searches Outer
before looking at those using
directives outside the namespace, so it finds Outer.Math
instead of System.Math
. Unfortunately (or perhaps fortunately?), Outer.Math
has no PI
member, so File1 is now broken.
This changes if you put the using
inside your namespace declaration, as follows:
// File1b.cs
namespace Outer.Inner
{
using System;
class Foo
{
static void Bar()
{
double d = Math.PI;
}
}
}
Now the compiler searches System
before searching Outer
, finds System.Math
, and all is well.
Some would argue that Math
might be a bad name for a user-defined class, since there's already one in System
; the point here is just that there is a difference, and it affects the maintainability of your code.
It's also interesting to note what happens if Foo
is in namespace Outer
, rather than Outer.Inner
. In that case, adding Outer.Math
in File2 breaks File1 regardless of where the using
goes. This implies that the compiler searches the innermost enclosing namespace before it looks at any using
directive.
using
directive in C# does not allow this:
Create a using directive to use the
types in a namespace without having to
specify the namespace. A using
directive does not give you access to
any namespaces that are nested in the
namespace you specify.
VB.NET, however, supports somewhat closer behavior with Imports
statement:
The scope of the elements made
available by an Imports statement
depends on how specific you are when
using the Imports statement. For
example, if only a namespace is
specified, all uniquely named members
of that namespace, and members of
modules within that namespace, are
available without qualification. If
both a namespace and the name of an
element of that namespace are
specified, only the members of that
element are available without
qualification.
Reference SO Question
Best Answer
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 justinputSequence.Where(...)
:)