Placing using before namespace{} or in namespace{} - What's the difference?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Empire City

    Placing using before namespace{} or in namespace{} - What's the difference?

    Can someone tell me what is the difference in placing the using in the
    namespace{} and before the namespace{} ?

    namespace MyProj
    {
    using System;
    using System.Web;
    }

    using System;
    using System.Web;
    namespace MyProj
    {
    }


  • Joe Mayo

    #2
    Re: Placing using before namespace{} or in namespace{} - What's the difference?

    Hi Empire,

    Comments in-line.

    "Empire City" <a@b.com> wrote in message
    news:Pwdub.2061 74$pT1.39657@tw ister.nyc.rr.co m...[color=blue]
    > Can someone tell me what is the difference in placing the using in the
    > namespace{} and before the namespace{} ?
    >
    > namespace MyProj
    > {
    > using System;
    > using System.Web;
    > }[/color]

    Here the using statements only apply to the code within the block defined by
    MyProj. If you added another namespace to the same file, like this:

    namespace MyOtherProj
    {
    class MyClass { Int32 myInt; }
    }

    You would get a compiler error "The type or namespace Int32 could not be
    found...".
    [color=blue]
    > using System;
    > using System.Web;
    > namespace MyProj
    > {
    > }[/color]

    On this one, the using statements apply to all code in this file. For
    example, if I defined:

    namespace MyOtherProj
    {
    }

    in the same file, I wouldn't need to re-define the using statements.

    I suppose if someone felt a need to define multiple namespaces in the same
    file, the second format would be useful.

    Joe
    --
    Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



    Comment

    • Empire City

      #3
      Re: Placing using before namespace{} or in namespace{} - What's the difference?

      OK, thank you. Makes perfect sense now. I think what threw me off was the
      Duwamish example puts the using within the namespace, while when you create
      a web form the usings are above the namespace.

      --

      "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
      news:uFgn1eYrDH A.2480@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi Empire,
      >
      > Comments in-line.
      >
      > "Empire City" <a@b.com> wrote in message
      > news:Pwdub.2061 74$pT1.39657@tw ister.nyc.rr.co m...[color=green]
      > > Can someone tell me what is the difference in placing the using in the
      > > namespace{} and before the namespace{} ?
      > >
      > > namespace MyProj
      > > {
      > > using System;
      > > using System.Web;
      > > }[/color]
      >
      > Here the using statements only apply to the code within the block defined[/color]
      by[color=blue]
      > MyProj. If you added another namespace to the same file, like this:
      >
      > namespace MyOtherProj
      > {
      > class MyClass { Int32 myInt; }
      > }
      >
      > You would get a compiler error "The type or namespace Int32 could not be
      > found...".
      >[color=green]
      > > using System;
      > > using System.Web;
      > > namespace MyProj
      > > {
      > > }[/color]
      >
      > On this one, the using statements apply to all code in this file. For
      > example, if I defined:
      >
      > namespace MyOtherProj
      > {
      > }
      >
      > in the same file, I wouldn't need to re-define the using statements.
      >
      > I suppose if someone felt a need to define multiple namespaces in the same
      > file, the second format would be useful.
      >
      > Joe
      > --
      > http://www.csharp-station.com
      >
      >[/color]


      Comment

      Working...