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

E

Empire City

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
{
}
 
J

Joe Mayo

Hi Empire,

Comments in-line.

Empire City said:
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;
}

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...".
using System;
using System.Web;
namespace MyProj
{
}

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
 
E

Empire City

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.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top