473,670 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using in Namespace

What is the differrence between using the "using" caluse outside of the
namespace definition and inside the namespace.

Example Outside:

using System;

namespace Example.Outside
{
}
Example Inside:

namespace Example.Inside
{
using System;
}

Any help is appreciated.
-rythm

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 1908
On 2004-01-03, Pranav Shah <ry******@yahoo .com> wrote:
What is the differrence between using the "using" caluse outside of the
namespace definition and inside the namespace.

Example Outside:

using System;

namespace Example.Outside
{
}
Example Inside:

namespace Example.Inside
{
using System;
}


Exactly what you probably think it is, the scope of the using statement
in the first example is the entire module, while the scope of the second
example is the braces. It really only matters if you put two namespaces
in a single file...

namespace Namespace1
{
using System.Collecti ons;

public class MyClass : ArrayList
{}
}

namespace Namespace2
{
public class MyClass2 : ArrayList
{}
}

This won't compile, because Namespace2 can't *see* the using statement.
--
David
dfoster at
hotpop dot com
Nov 15 '05 #2

"Pranav Shah" <ry******@yahoo .com> wrote in message
news:OQ******** ******@TK2MSFTN GP09.phx.gbl...

What is the differrence between using the "using" clause outside
of the namespace definition and inside the namespace.

Example Outside:

using System;

namespace Example.Outside
{
}
Example Inside:

namespace Example.Inside
{
using System;
}


Use of the 'using' within a namespace restricts the scope of the 'exposed'
symbols to that namespace, whilst use of it outside a namespace imposes no
such restriction.

For example, doing this:

// sample1.cs
using System;

namespace One
{
public class X { void print() { Console.WriteLi ne("..."); } }
}

namespace Two
{
public class X { void print() { Console.WriteLi ne("..."); } }
}

public class Program
{
public static void Main(String[] args) { Console.WriteLi ne("..."); }
}

// ----------------

allows the single 'using System;' statement to expose the 'System'
namespace's symbols to all entities in the source file. On the other hand,
doing this:

// sample2.cs
namespace One
{
using System;
public class X { void print() { Console.WriteLi ne("..."); } }
}

namespace Two
{
using System;
public class X { void print() { Console.WriteLi ne("..."); } }

public class Program
{
public static void Main(String[] args) { Console.WriteLi ne("..."); }
}
}

// ----------------

exposes 'System' namespace symbols only within each namespace. It also, by
the by, forces the need to move the declaration of 'Program' inside one of
the namespaces unless a 'using System' is placed outside of, and before, any
other namespace elements.

I would say that for maximum control over namespace symbol exposure [hence
minimimse possibility of name-clashes] that 'using' clauses be placed within
namespace declarations. This is, of course, easily accomplished if you write
all your code using namespaces. It is, also, merely a possible stylistic
practice, not a hard and fast coding rule.

I hope this helps.

Anthony Borla
Nov 15 '05 #3
Hai Friend
In your first code you are adding the lib reference earleir but in your
second code - you have to mention it each and everytime whenever you use
classes from system..//

Muthukumar
"Pranav Shah" <ry******@yahoo .com> wrote in message
news:OQ******** ******@TK2MSFTN GP09.phx.gbl...
What is the differrence between using the "using" caluse outside of the
namespace definition and inside the namespace.

Example Outside:

using System;

namespace Example.Outside
{
}
Example Inside:

namespace Example.Inside
{
using System;
}

Any help is appreciated.
-rythm

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4

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

Similar topics

5
1948
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
8
4907
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using namespace std;' unless the std namespace was declared first. This triggered my curiosity, and I tried to find out what the ANSI C++ standard had to say about this. I'm unable to find a conclusion, and hope someone here have a clue to spare. ...
0
1868
by: Simon | last post by:
I need call a LoginUser API from MC++ dll, but when I try to call the I have always the same exception: "System.NullReferenceException: Object reference not set to an instance of an object. at LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, Int32 dwLogonType, Int32 dwLogonProvider, IntPtr phToken)" Thanks in advance
3
1469
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in File1.cs of the following code. Notice that when the using keyword is placed outside of the namespace decleration its behavior is different than placing it inside the namespace decleration. Questions: 1) Section 9.3.2 of the specification...
8
2406
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL into the bin directory but am not able to progress. Can anyone please guide me to an online tutorial on the subject. Thanks,
12
2839
by: Keith Patrick | last post by:
Can someone tell me the difference in terms of actual implications using: namespace MyNamespace { using System; class MyClass {...} } vs. using System;
30
4099
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
3
1915
by: Wayne Shu | last post by:
When I read the chapter of the namespace of the book C++ Primer(3e). It explain the using directive as follow: "A using directive makes the namespace member names visible as if they were declared outside the namespace at the location where the namespace definition is located." I have some doubt about the using directives for the nested namespace, so I wrote a program to test it, #include <iostream>
2
4399
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it (before I incorporate them in another program). I just can’t seem to get a non-static call to work in my thread that has access to the Form1 variables and controls I need. I can call a non-static function using another class but then I can seem to get in...
0
8384
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
8901
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
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
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
7412
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...
0
4208
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...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.