using inside or outside my namespace

  • Thread starter Thread starter Digital Fart
  • Start date Start date
D

Digital Fart

I was looking at the GFAX - Gnome fax application and i see
that in the main.cs the author puts the using inside his namespace.

namespace gfax {
using System;
using System.IO;
using System.Collections;
....

But in all the tutorial i have seen they start with using and then
start their namespace.

What would be the difference and/or advantage?

Joeri
 
(Line wrap alert - didn't fix them)


//***************************************
using System.Text;

namespace ConsoleApp
{
using System.IO;

class Class1
{
[STAThread]
static void Main(string[] args)
{
string strPath =
Directory.GetParent("C:\\Documents and Settings\\A
User").FullName;
StringBuilder strBuilder = new
StringBuilder(strPath);
strBuilder.Append("\\test.xml");
System.Xml.XmlDocument objXml =
new System.Xml.XmlDocument();
objXml.Load(strPath);
}
}
}

namespace ConsoleApp.Test
{
using System.Xml;

public class Class2
{
public Class2()
{
string strPath =
System.IO.Directory.GetParent("C:\\Documents and
Settings\\A User").FullName;
StringBuilder strBuilder = new
StringBuilder(strPath);
strBuilder.Append("\\test.xml");
XmlDocument objXml = new
XmlDocument();
objXml.Load(strPath);
}
}
}

//***************************************************
 
In addition to what everybody else has pointed out, it also affects name
lookup. When the using clauses are inside the namespace, they will be
searched before the outer namespace is searched. When the using clauses are
outside the namespace, they will be searched after.

Example:
// file1.cs
namespace N1 {
namespace N2 {
class C1 { ... }
}
}
class C1 { ... }

// file2.cs
using N1.N2;
namespace N1 {
// Any code here that references C1 in here will get the outer C1
}

// file2.cs
namespace N1 {
using N1.N2;
// Any code here that references C1 in here will get N1.N2.C1
}
 
Back
Top