Placement of the using keyword

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Brian Gideon (briangideon_at_yahoo.com)
Date: 03/11/05


Date: 11 Mar 2005 10:07:36 -0800

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



Relevant Pages

  • Re: Placement of the using keyword
    ... > placement of the using keyword were limited to normal scoping rules. ... > I suspect you may be right about rolling up the namespace hierarchy. ... > Sean Hederman wrote: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Placement of the using keyword
    ... "Brian Gideon" wrote in message ... Notice that when the using keyword is placed outside ... > inside the namespace decleration. ... > in the placement of the using keyword. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Placement of the using keyword
    ... placement of the using keyword were limited to normal scoping rules. ... I suspect you may be right about rolling up the namespace hierarchy. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A query on C#
    ... > aa.aa obja = new aa.aa; ... > keyword for the namespace 'aa'. ... The simple name aa now refers to the class aa.aa and not to the namespace ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Matz says namespaces are too hard to implement - why?
    ... invoke that method from another method invoked in that namespace the ... via a keyword or other syntax, the overhead of checking for a namespace ... class StringDecorate ...
    (comp.lang.ruby)