473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Placement of the using keyword

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
3 1452
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
8
by: elviin | last post by:
Hello. I tried a sample programm using placement new. I am not sure if i understand this technique correctly. I do not mean usage but a real world application. Could anyone to describe some...
6
by: Marc Mutz | last post by:
Hi, I'm in discussion with a lib vendor who uses placement new to forward construction to another ctor, like this: MyClass( ... ) { new (this) MyClass( ... ); } I asked them to use a private...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
5
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.