Using in Namespace

  • Thread starter Thread starter Pranav Shah
  • Start date Start date
P

Pranav Shah

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
 
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.
 
Pranav Shah said:
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
 
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
 
Back
Top