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

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 1894
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.Collections;

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**************@TK2MSFTNGP09.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.WriteLine("..."); } }
}

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

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

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

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.WriteLine("..."); } }
}

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

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

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

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**************@TK2MSFTNGP09.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
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
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...
0
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...
3
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...
8
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...
12
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
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" <<...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
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...
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...
0
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...
0
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...
0
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,...

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.