473,786 Members | 2,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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.Collecti ons;

namespace MyNamespace
{
...
}

and

namespace MyNamespace
{
using System;
using System.Collecti ons;

...
}

What is the diference between the two?

Thanks,
Ben
Jan 14 '07 #1
7 1641
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.co m>
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.Reg ularExpressions ;
//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.Reg ularExpressions ;
//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*@discussion s.microsoft.com wrote in message
news:E4******** *************** ***********@mic rosoft.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.Collecti ons;

namespace MyNamespace
{
...
}

and

namespace MyNamespace
{
using System;
using System.Collecti ons;

...
}

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
2637
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
7324
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 provider. Precisely, here is the code i have Dim Cmdobj As New ADODB.Command Cmdobj.ActiveConnection = oconn Cmdobj.CommandType = adCmdStoredProc
2
1522
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
2815
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" statements because its DDL. Anyone know a way around this? --- Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
1
2390
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 look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
1
3185
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). The README file in the Tools/freeze directory of the Python-2.4.4 distribution says the following (and many other things): Previous versions of Freeze used a pretty simple-minded algorithm to
14
2900
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 statement. I first connect to the database... self.con = MySQLdb.connect(user=username, passwd =password) self.cursor = self.con.cursor() self.cursor.execute("SET max_error_count=0")
2
3398
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 worked correctly and 1 failed: The one that failed didn't have mysql_real_escape_string and neither did 2 of the ones that worked: in those 2 I used prepared sql statements (PEAR DB package). The other that I used was with...
3
1925
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 to put everything in there. Why can't the using command just allow something like this? using (Pen mypen = new Pen(Color.Black), Brush mybrush = new SolidBrush(Color.Blue)) { }
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.