By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Using in Namespace

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.