473,386 Members | 1,745 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,386 software developers and data experts.

'using' statements

Hi,

I sometimes see code where the 'using' statements at the top of the file are
located
before the namespace declaration, and sometimes they are located inside it.
For example:

using System;
using System.Collections;

namespace MyNamespace
{
...
}

and

namespace MyNamespace
{
using System;
using System.Collections;

...
}

What is the diference between the two?

Thanks,
Ben
Jan 14 '07 #1
7 1600
Ben wrote:
I sometimes see code where the 'using' statements
These are using declarations, not using statements. Using statements are
for resource protection; using declarations are for importing
namespaces.
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?
If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration. E.g., in a
single file:

---8<---
namespace X
{
using Y; // limits Y to this declaration of X
}

namespace X
{
// members of Y namespace are not in scope here
}
--->8---

I'm not aware of any other differences.

-- Barry

--
http://barrkel.blogspot.com/
Jan 14 '07 #2
Barry Kelly <ba***********@gmail.comwrote:
I sometimes see code where the 'using' statements

These are using declarations, not using statements. Using statements are
for resource protection; using declarations are for importing
namespaces.
They're actually directives, using the terminology of the spec :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 14 '07 #3
Jon Skeet wrote:
Barry Kelly <ba***********@gmail.comwrote:
I sometimes see code where the 'using' statements
These are using declarations, not using statements. Using statements are
for resource protection; using declarations are for importing
namespaces.

They're actually directives, using the terminology of the spec :)
Yes... I've been living in Delphi-land (+ C) again recently owing to new
job at CodeGear, and Delphi's "directive"s are something else, so that
word has had its meaning nudged over a bit in my little head :)

-- Barry

--
http://barrkel.blogspot.com/
Jan 14 '07 #4
Barry Kelly wrote:
I sometimes see code where the 'using' statements
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?

If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration.
I'm not aware of any other differences.
One related difference is that using directives can't 'see' directives
in the same scope, but *can* see directives in an outer scope.

For example,

namespace Outer
{
using Regexen = System.Text.RegularExpressions;
//using Regex = Regexen.Regex; // can't see Regexen, here
namespace Inner
{
using Regex = Regexen.Regex;
}
}

The commented-out alias statement in the Outer namespace wouldn't
compile, because it can't 'see' the alias in its scope. However, the
same directive will compile in the Outer.Inner namepscae, because it
can see the alias.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Jan 15 '07 #5
Barry Kelly wrote:
I sometimes see code where the 'using' statements
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?

If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration.
I'm not aware of any other differences.
One related difference is that using directives can't 'see' directives
in the same scope, but *can* see directives in an outer scope.

For example,

namespace Outer
{
using Regexen = System.Text.RegularExpressions;
//using Regex = Regexen.Regex; // can't see Regexen, here
namespace Inner
{
using Regex = Regexen.Regex;
}
}

The commented-out alias statement in the Outer namespace wouldn't
compile, because it can't 'see' the alias in its scope. However, the
same directive will compile in the Outer.Inner namespace, because it
can see the alias.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Jan 15 '07 #6
Ben,

There is one more subtle difference that Barry didn't mention.
Consider the following example and notice how the *same* statement
produces a completely different effect. I believe the key part in the
specification that describes this behavior is in section 10.7 (ECMA),
but the wording is very confusing. Maybe someone can verify that?

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

Brian

Jan 15 '07 #7
I guess this has to do with the VS.
If you use an alias inside the namespace and then use intellisense to update
the reference to other namespaces it will add the new using statements below
your alias.

Regards
Sandeep

"Ben" <Be*@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...
Hi,

I sometimes see code where the 'using' statements at the top of the file
are
located
before the namespace declaration, and sometimes they are located inside
it.
For example:

using System;
using System.Collections;

namespace MyNamespace
{
...
}

and

namespace MyNamespace
{
using System;
using System.Collections;

...
}

What is the diference between the two?

Thanks,
Ben

Apr 13 '07 #8

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

Similar topics

9
by: Jaime Wyant | last post by:
I know I've seen this somewhere, but can't seem to google it. Is there a way to use an alternate statement separator, other than the default ';'? jw
2
by: Yves Touze | last post by:
Hi All, I'm trying to migrate from SQL Server 7.0 to SQL Server 2000. I've got some ASP page which call VB components that retrieve shaped recordsets from SQL Server using the MSDATASHAPE...
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;
1
by: rudykayna | last post by:
I'm having trouble executing multiple DDL statements in one SQL file. I've been using ExecuteNonQuery() but it does not seem to like the "GO" statements in my SQL file. I need to keep the "GO"...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
2
by: Pugi! | last post by:
It is by accident that I noticed that I forgot to use mysql_real_escape_string in part of my webapp. I tested input with following text : Hélène 51°56'12'' http://www.mysite.org/folder 3 functions...
3
by: Israel | last post by:
I always wondered why the using command can only take one object. I always find that this isn't sufficient for drawing so I end up always disposing in my finally block but then I have to remember...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.