namespaces

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

Are these equivalent?

------------------
using System;

namespace SomeNamespace
{

}
 
Are these equivalent?

------------------
using System;

namespace SomeNamespace
{

}

-------------------
namespace SomeNamespace
{
    using System;



}

In that particular case, yes. But, like Jon said, it can make a
difference. I discovered this oddity and posted it in this group
quite some time ago hoping someone could point me to the section in
the specification that discusses it. I did some scouring of my own
and I believe the crucial section is 10.7 (ECMA) where it says:

"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."

Clear as mud huh?
 
Not quite, or at least not always.Seehttp://blogs.msdn.com/ericlippert/archive/2007/06/25/inside-or-outsid...

Jon

Don't you have a web page of C# oddities? This would almost certainly
qualify to be listed!
 
Brian Gideon said:
Yep, that's the link I was referring too.

Okay - I'll try to remember to do it soonish. (At the very least I'll
add an HTML comment to remind me :)
 
Here's an example teaser. It demonstrates how the placement of the
using statement can create an even more peculiar effect than what the
blog elluded to. Notice how the inner placed version imports the A.B
type even though it was contained within A.C. That means it is doing
more than giving preference to a nested namespace. It must be
*walking up* the containing namespace path as well as demonstrated
below. Peculiar indeed!

using B; // Try it here

namespace A.C
{
using B; // and then here next!

public class Program
{
public static void Main(string[] args)
{
Foo.DoSomething();
}
}
}

namespace A.B
{
public class Foo
{
public static void DoSomething()
{
Console.WriteLine("A.B.Foo.DoSomething()");
}
}
}

namespace B
{
public class Foo
{
public static void DoSomething()
{
Console.WriteLine("B.Foo.DoSomething()");
}
}
}
 
Brian Gideon said:
Here's an example teaser. It demonstrates how the placement of the
using statement can create an even more peculiar effect than what the
blog elluded to. Notice how the inner placed version imports the A.B
type even though it was contained within A.C. That means it is doing
more than giving preference to a nested namespace. It must be
*walking up* the containing namespace path as well as demonstrated
below. Peculiar indeed!

Ooh, that will certainly make my life easier. Thanks very much :)
 
"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."

so which one is better? or perferred?


Are these equivalent?

------------------
using System;

namespace SomeNamespace
{

}

-------------------
namespace SomeNamespace
{
using System;



}

In that particular case, yes. But, like Jon said, it can make a
difference. I discovered this oddity and posted it in this group
quite some time ago hoping someone could point me to the section in
the specification that discusses it. I did some scouring of my own
and I believe the crucial section is 10.7 (ECMA) where it says:

"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."

Clear as mud huh?
 
Get the tattoo:
THE MOST READABLE CODE WINS.

Tem said:
"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."

so which one is better? or perferred?




In that particular case, yes. But, like Jon said, it can make a
difference. I discovered this oddity and posted it in this group
quite some time ago hoping someone could point me to the section in
the specification that discusses it. I did some scouring of my own
and I believe the crucial section is 10.7 (ECMA) where it says:

"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."

Clear as mud huh?
 
so which one is better? or perferred?

I'm not sure it matters really. I guess there is a strong argument
for putting the using statement outside the namespace declaration
since that yields the most intuitive result.

I actually stumbled upon this on my own several years ago because I
was putting the using statement inside the namespace declaration and
actually had this strange situation (which I posted as an example in
this thread in a response to Jon) occur once. It was a very difficult
bug to find because, at that time, I felt like the behavior of the
using statement was so fundamentally established to me that I didn't
even entertain the possiblity that it was the root of my problem. I
was lead down the wrong path of thinking there was some obscure bug in
the compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top