Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

namespaces

2 views
Skip to first unread message

Tem

unread,
May 6, 2008, 4:11:30 AM5/6/08
to
Are these equivalent?

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

namespace SomeNamespace
{

}

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

Jon Skeet [C# MVP]

unread,
May 6, 2008, 6:14:25 AM5/6/08
to

Brian Gideon

unread,
May 6, 2008, 1:11:56 PM5/6/08
to

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?

Brian Gideon

unread,
May 6, 2008, 1:14:36 PM5/6/08
to
On May 6, 5:14 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> 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!

Jon Skeet [C# MVP]

unread,
May 6, 2008, 1:44:01 PM5/6/08
to
Brian Gideon <brian...@yahoo.com> wrote:
> On May 6, 5:14 am, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> > Not quite, or at least not always.Seehttp://blogs.msdn.com/ericlippert/archive/2007/06/25/inside-or-outsid...
>
> Don't you have a web page of C# oddities? This would almost certainly
> qualify to be listed!

I've got this one:

http://pobox.com/~skeet/csharp/teasers.html

if that's what you mean. Yes, I could add it - along with a whole bunch
I suspect I've got queued up somewhere!

--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Brian Gideon

unread,
May 6, 2008, 2:40:53 PM5/6/08
to
On May 6, 12:44 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:

Yep, that's the link I was referring too.

Jon Skeet [C# MVP]

unread,
May 6, 2008, 2:47:23 PM5/6/08
to
Brian Gideon <brian...@yahoo.com> wrote:
> > I've got this one:
> >
> > http://pobox.com/~skeet/csharp/teasers.html
> >
> > if that's what you mean. Yes, I could add it - along with a whole bunch
> > I suspect I've got queued up somewhere!
>
> 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 :)

Brian Gideon

unread,
May 6, 2008, 4:28:34 PM5/6/08
to
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()");
}
}
}


Jon Skeet [C# MVP]

unread,
May 6, 2008, 4:40:03 PM5/6/08
to
Brian Gideon <brian...@yahoo.com> wrote:
> 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 :)

Tem

unread,
May 9, 2008, 1:58:46 AM5/9/08
to
"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?


"Brian Gideon" <brian...@yahoo.com> wrote in message
news:f4cde640-6059-489b...@l42g2000hsc.googlegroups.com...

clintonG

unread,
May 9, 2008, 10:38:25 AM5/9/08
to
Get the tattoo:
THE MOST READABLE CODE WINS.

"Tem" <tem...@yahoo.com> wrote in message
news:OqplAnZs...@TK2MSFTNGP03.phx.gbl...

Brian Gideon

unread,
May 9, 2008, 2:39:36 PM5/9/08
to
On May 9, 12:58 am, "Tem" <tem1...@yahoo.com> wrote:
> 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.

0 new messages