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

using inside or outside my namespace

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Joeri,

The using directive can be used in a namespace scope, in which case it
is applicable to the namespace in that file. If it is outside of the
namespace, then it is applicable to the whole file.

For more information, check out section 9.3 of the C# Language
Specification, titled "Using Directives", located at (watch for line wrap):

http://msdn.microsoft.com/library/de...rpspec_9_3.asp

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:32********************************@4ax.com...
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

Nov 13 '05 #2

P: n/a
(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);
}
}
}

//************************************************** *
-----Original Message-----
I was looking at the GFAX - Gnome fax application and i seethat 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 thenstart their namespace.

What would be the difference and/or advantage?

Joeri
.

Nov 13 '05 #3

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

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"Digital Fart" <pr*********@angelfire.com> wrote in message
news:32********************************@4ax.com...
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

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.