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