By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,236 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,236 IT Pros & Developers. It's quick & easy.

Placement of the using keyword

P: n/a
I stumbled across something odd today about the placement of the using
keyword. Section 9.3.2 of the C# v1.1 specification did not answer my
question. My confusion is isolated to what happens in File1.cs of the
following code. Notice that when the using keyword is placed outside
of the namespace decleration its behavior is different than placing it
inside the namespace decleration.

Questions:

1) Section 9.3.2 of the specification does not mention the differences
in the placement of the using keyword. Why? Is it described somewhere
else in the specification?

2) How does the compiler know to import A.B types as opposed to B types
when "using B" is contained within namespace A.C.D? Does the using
keyword somehow search the namespace hierarchy it is contained within
(in this case A.C.D) to find a match first and then search outside as a
last resort?

// File1.cs
// 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;
}

// File2.cs
namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

// File3.cs
namespace B
{
public class Bar1 { }
public class Bar2 { }
}

Thanks,
Brian

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I stumbled across something odd today about the placement of the using
keyword. Section 9.3.2 of the C# v1.1 specification did not answer my
question. My confusion is isolated to what happens in File1.cs of the
following code. Notice that when the using keyword is placed outside
of the namespace decleration its behavior is different than placing it
inside the namespace decleration.

Questions:

1) Section 9.3.2 of the specification does not mention the differences
in the placement of the using keyword. Why? Is it described somewhere
else in the specification?
Just had a look at Helsbergs book, and it appears that the only difference
in the placement relates to the scope of aliases:

// File1.cs
using R = N1.N2; // N1.N2 contains a class called OtherClass

namespace A {
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // OK
}
}

// File2.cs
namespace A {
using R = N1.N2;
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // Not OK
}
}
2) How does the compiler know to import A.B types as opposed to B types
when "using B" is contained within namespace A.C.D? Does the using
keyword somehow search the namespace hierarchy it is contained within
(in this case A.C.D) to find a match first and then search outside as a
last resort?
Interesting, from what I can see in the documentation, using B in A.C.D
should import B & A.C.D.B. If you're right (and I assume you are), it
probably means that it rolls up the namespace hierarchy: importing B & A.B &
A.C.B & A.C.D.B.

// File1.cs
// 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;
}

// File2.cs
namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

// File3.cs
namespace B
{
public class Bar1 { }
public class Bar2 { }
}

Thanks,
Brian

Nov 16 '05 #2

P: n/a
Sean,

Thanks for the feedback. I also noticed that the differences in
placement of the using keyword were limited to normal scoping rules.
Section 9.3.1 is basically what you've cited.

I suspect you may be right about rolling up the namespace hierarchy.
But you'd think that would be mentioned somewhere in the specification.
If it isn't then we can only make assumptions based on observations.

Brian

Sean Hederman wrote:
1) Section 9.3.2 of the specification does not mention the
differences in the placement of the using keyword. Why?
Is it described somewhere else in the specification?


Just had a look at Helsbergs book, and it appears that the
only difference in the placement relates to the scope of
aliases:

// File1.cs
using R = N1.N2; // N1.N2 contains a class called OtherClass

namespace A {
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // OK
}
}

// File2.cs
namespace A {
using R = N1.N2;
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // Not OK
}
}
2) How does the compiler know to import A.B types as
opposed to B types when "using B" is contained within
namespace A.C.D? Does the using keyword somehow
search the namespace hierarchy it is contained within
(in this case A.C.D) to find a match first and then
search outside as a last resort?


Interesting, from what I can see in the documentation,
using B in A.C.D should import B & A.C.D.B. If you're
right (and I assume you are), it probably means that it
rolls up the namespace hierarchy: importing B & A.B &
A.C.B & A.C.D.B.


Nov 16 '05 #3

P: n/a
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Sean,

Thanks for the feedback. I also noticed that the differences in
placement of the using keyword were limited to normal scoping rules.
Section 9.3.1 is basically what you've cited.

I suspect you may be right about rolling up the namespace hierarchy.
But you'd think that would be mentioned somewhere in the specification.
If it isn't then we can only make assumptions based on observations.
Probably is mentioned in subsection n, clause 18, paragraph 216 or
something. You'd probably have to do a full lexical analysis just to find
anything related ;D
Brian

Sean Hederman wrote:
> 1) Section 9.3.2 of the specification does not mention the
> differences in the placement of the using keyword. Why?
> Is it described somewhere else in the specification?


Just had a look at Helsbergs book, and it appears that the
only difference in the placement relates to the scope of
aliases:

// File1.cs
using R = N1.N2; // N1.N2 contains a class called OtherClass

namespace A {
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // OK
}
}

// File2.cs
namespace A {
using R = N1.N2;
class Class1 : R.OtherClass { // OK
}
}
namespace B {
class Class2 : R.OtherClass { // Not OK
}
}
> 2) How does the compiler know to import A.B types as
> opposed to B types when "using B" is contained within
> namespace A.C.D? Does the using keyword somehow
> search the namespace hierarchy it is contained within
> (in this case A.C.D) to find a match first and then
> search outside as a last resort?


Interesting, from what I can see in the documentation,
using B in A.C.D should import B & A.C.D.B. If you're
right (and I assume you are), it probably means that it
rolls up the namespace hierarchy: importing B & A.B &
A.C.B & A.C.D.B.

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.