Difference between namespace { using System; } vs. using System; namespace {}

  • Thread starter Thread starter Keith Patrick
  • Start date Start date
K

Keith Patrick

Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;

class MyClass {...}
}

vs.

using System;
namespace MyNamespace {

class MyClass {...}
}

are? There's a framework recommendation that the former be used (and some
ASP.Net templates create classes like this), but I use the latter. I've
tried both but could never determine a difference between the two, so I'm
unsure why there's a recommendation to do one over the other.
 
I'd think that if you had another namespace { } after the one you
defined, the former woudl keep the using confied to the first
namespace, while the latter would be global to the file.

Where did you see that the latter is prefered? None of the temples
when you create a new class have the using within the namespace; its
always been the outside.

Andy
 
I am guessing, but could it have anything to do with the scope of the
using statement. I use the latter one all the time myself.

I will be following this thread with interest to figure out the actual
reasons.

- Vaibhav
http://www.nagarro.com
 
Keith,

Consider this example:

// The following line imports namespace B types (Bar1 & Bar2).
using B;
namespace A.C.D
{
// The following line imports namespace A.B types (Foo1 & Foo2).
using B;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}


As you can see changing the location of the using statement changes
what it is imported. I believe the formal explanation is in section
10.7 of the C# specification (ECMA version).

"The scope of a namespace member declared by a
namespace-member-declaration
within a namespace-declaration whose fully qualified name is N, is the
namespace-body of every namespace-declaration whose fully qualified
name is N
or starts with N, followed by a period."

That is quite possibly the most confusing sentence I've ever read!

Brian
 
Have you tried it? For me intellisence shows Foo methods for both
declaration of B namespace, either outside or inside the A.C.D namespace,
which means that A.B is imported in both cases.
That's definitely not what I would expect.
 
Sericinus said:
Have you tried it?

I tried it in both 1.1 and 2.0 with the same result.
For me intellisence shows Foo methods for both
declaration of B namespace, either outside or inside the A.C.D namespace,
which means that A.B is imported in both cases.

My intellisense worked correctly. Did you try compiling it?
That's definitely not what I would expect.

Yeah, it's not what I would expect either, but I think the C#
specification agrees with the behavior observed.
 
Sericinus said:
Well, it also compiles fine when I use Foo, in both cases.

Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.
 
Brian said:
Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.

Here it is:

using B;
namespace A.C.D
{
//using B;
class acd
{
acd()
{
int i = B.ab.aba;
}
}
}

namespace B
{
class b
{
static public int ba;
}
}

namespace A.B
{
class ab
{
static public int aba;
}
}
 
Okay. I see why it compiles now. You're qualifying the class ab with
the partial namespace identifier B. The behavior is exactly the same
as placing "using B" inside the namespace A.C.D and referring to class
ab directly. That makes sense and is in agreement with the
specification.

Try changing:

int i = B.ab.aba;

to

int i = ab.aba;

Then play around with the placement of the using statement.
 
You are right.

Brian said:
Okay. I see why it compiles now. You're qualifying the class ab with
the partial namespace identifier B. The behavior is exactly the same
as placing "using B" inside the namespace A.C.D and referring to class
ab directly. That makes sense and is in agreement with the
specification.

Try changing:

int i = B.ab.aba;

to

int i = ab.aba;

Then play around with the placement of the using statement.
 
asp.net web controls embed the using section in the namespace.
The place I saw it was in a Brad Abrams blog post regarding the .Net
conventions, and that was one. But unfortunately, the rationale wasn't
placed in the rule, so I couldn't figure out why it was recommended.
 
Back
Top