473,326 Members | 2,173 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,326 software developers and data experts.

Difference between namespace { using System; } vs. using System; namespace {}

Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;

class MyClass {...}
}

vs.

using System;
namespace MyNamespace {

class MyClass {...}
}

are? There's a framework recommendation that the former be used (and some
ASP.Net templates create classes like this), but I use the latter. I've
tried both but could never determine a difference between the two, so I'm
unsure why there's a recommendation to do one over the other.
May 4 '06 #1
12 2800
I'd think that if you had another namespace { } after the one you
defined, the former woudl keep the using confied to the first
namespace, while the latter would be global to the file.

Where did you see that the latter is prefered? None of the temples
when you create a new class have the using within the namespace; its
always been the outside.

Andy

May 4 '06 #2
V
I am guessing, but could it have anything to do with the scope of the
using statement. I use the latter one all the time myself.

I will be following this thread with interest to figure out the actual
reasons.

- Vaibhav
http://www.nagarro.com

May 4 '06 #3
Keith,

Consider this example:

// 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;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}
As you can see changing the location of the using statement changes
what it is imported. I believe the formal explanation is in section
10.7 of the C# specification (ECMA version).

"The scope of a namespace member declared by a
namespace-member-declaration
within a namespace-declaration whose fully qualified name is N, is the
namespace-body of every namespace-declaration whose fully qualified
name is N
or starts with N, followed by a period."

That is quite possibly the most confusing sentence I've ever read!

Brian

Keith Patrick wrote:
Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;

class MyClass {...}
}

vs.

using System;
namespace MyNamespace {

class MyClass {...}
}

are? There's a framework recommendation that the former be used (and some
ASP.Net templates create classes like this), but I use the latter. I've
tried both but could never determine a difference between the two, so I'm
unsure why there's a recommendation to do one over the other.


May 4 '06 #4
Have you tried it? For me intellisence shows Foo methods for both
declaration of B namespace, either outside or inside the A.C.D namespace,
which means that A.B is imported in both cases.
That's definitely not what I would expect.

Brian Gideon wrote:
Keith,

Consider this example:

// 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;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}

May 5 '06 #5
can you definetely say that it is not just a bug in intellisense?

--
"Sericinus hunter" <se*****@flash.net> schrieb im Newsbeitrag
news:eA***************@newssvr33.news.prodigy.com. ..
Have you tried it? For me intellisence shows Foo methods for both
declaration of B namespace, either outside or inside the A.C.D namespace,
which means that A.B is imported in both cases.
That's definitely not what I would expect.

Brian Gideon wrote:
Keith,

Consider this example:

// 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;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}

May 5 '06 #6
cody wrote:
can you definetely say that it is not just a bug in intellisense?


Well, it also compiles fine when I use Foo, in both cases.
May 5 '06 #7

Sericinus hunter wrote:
Have you tried it?
I tried it in both 1.1 and 2.0 with the same result.
For me intellisence shows Foo methods for both
declaration of B namespace, either outside or inside the A.C.D namespace,
which means that A.B is imported in both cases.
My intellisense worked correctly. Did you try compiling it?
That's definitely not what I would expect.


Yeah, it's not what I would expect either, but I think the C#
specification agrees with the behavior observed.

May 5 '06 #8

Sericinus hunter wrote:
cody wrote:
can you definetely say that it is not just a bug in intellisense?


Well, it also compiles fine when I use Foo, in both cases.


Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.

May 5 '06 #9
Brian Gideon wrote:
Sericinus hunter wrote:
cody wrote:
can you definetely say that it is not just a bug in intellisense?

Well, it also compiles fine when I use Foo, in both cases.


Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.


Here it is:

using B;
namespace A.C.D
{
//using B;
class acd
{
acd()
{
int i = B.ab.aba;
}
}
}

namespace B
{
class b
{
static public int ba;
}
}

namespace A.B
{
class ab
{
static public int aba;
}
}
May 5 '06 #10
Okay. I see why it compiles now. You're qualifying the class ab with
the partial namespace identifier B. The behavior is exactly the same
as placing "using B" inside the namespace A.C.D and referring to class
ab directly. That makes sense and is in agreement with the
specification.

Try changing:

int i = B.ab.aba;

to

int i = ab.aba;

Then play around with the placement of the using statement.

Sericinus hunter wrote:
Brian Gideon wrote:
Sericinus hunter wrote:
cody wrote:
can you definetely say that it is not just a bug in intellisense?
Well, it also compiles fine when I use Foo, in both cases.


Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.


Here it is:

using B;
namespace A.C.D
{
//using B;
class acd
{
acd()
{
int i = B.ab.aba;
}
}
}

namespace B
{
class b
{
static public int ba;
}
}

namespace A.B
{
class ab
{
static public int aba;
}
}


May 5 '06 #11
You are right.

Brian Gideon wrote:
Okay. I see why it compiles now. You're qualifying the class ab with
the partial namespace identifier B. The behavior is exactly the same
as placing "using B" inside the namespace A.C.D and referring to class
ab directly. That makes sense and is in agreement with the
specification.

Try changing:

int i = B.ab.aba;

to

int i = ab.aba;

Then play around with the placement of the using statement.

Sericinus hunter wrote:
Brian Gideon wrote:
Sericinus hunter wrote:
cody wrote:
> can you definetely say that it is not just a bug in intellisense?
Well, it also compiles fine when I use Foo, in both cases.
Can you post your code? When put "using B" outside of namespace A.C.D
I get a compile error when I try to use Foo inside of namespace A.C.D.

Here it is:

using B;
namespace A.C.D
{
//using B;
class acd
{
acd()
{
int i = B.ab.aba;
}
}
}

namespace B
{
class b
{
static public int ba;
}
}

namespace A.B
{
class ab
{
static public int aba;
}
}

May 5 '06 #12
asp.net web controls embed the using section in the namespace.
The place I saw it was in a Brad Abrams blog post regarding the .Net
conventions, and that was one. But unfortunately, the rationale wasn't
placed in the rule, so I couldn't figure out why it was recommended.
May 10 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cody | last post by:
What is the difference between OleDBConnection and SqlConnection? Or better expressed, what is OleDB? -- cody www.deutronium.de.vu || www.deutronium.tk
1
by: BH | last post by:
Is there a performance difference between importing (keyword "using") a namespace for an entire class file and using the classes from that namespace with the fully qualified name? Particularly...
2
by: Empire City | last post by:
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;
4
by: SAI | last post by:
Both have "TextBox" element and "Runat server". I don't understand the difference. Please advise. Thanks.
1
by: Shadow Lynx | last post by:
If this is not the most appropriate group for this question, please let me know. I noticed an odd priority difference in resolving names in VS 2005 (VWD Express) vs. the .NET 2.0 compiler (the...
7
by: Bart_D | last post by:
Hi, Can anybody explain me what's the difference between for example: imports system.data implements ICallbackEventHandler inherits System.Web.UI.Page Thanks Bart
2
by: Beemer Biker | last post by:
I have some demo code that came with a 3rd party product. This is what it looks like (I remove html braces in case it does not print right) Register TagPrefix="pin"...
6
by: =?Utf-8?B?SmVmZg==?= | last post by:
I thought this would already be covered here, but my search turned up nothing. In VS2005, if I use "String" to define a new variable/class, it colors it in the Aqua color as it does other...
3
by: tshad | last post by:
I have a file that I converted from VB.Net to C# that works fine in VB.Net when I compile but not in C# using the same libraries. The error I am getting is: PageInit.cs(9,7): error CS0138: A...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.