using inside or outside my namespace

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
 
N

Nicholas Paldino [.NET/C# MVP]

M

Mike M.

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

//***************************************************
 
G

Grant Richins [MS]

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top