473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

using inside or outside my namespace

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
3 7616
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
(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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: qazmlp | last post by:
One of the implementation file(.C) file has the following code namespace Utils { // declaration & definition of some functions } Is this way having named namespace( inside a .C file...
3
by: Imre | last post by:
As far as I know, the using directive is transitive, and I'm not sure I like this. So I'd like to ask if there's any workaround. Consider this: namespace MyNameSpace { void...
3
by: Pranav Shah | last post by:
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 { }
3
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
12
by: Keith Patrick | last post by:
Can someone tell me the difference in terms of actual implications using: namespace MyNamespace { using System; class MyClass {...} } vs. using System;
2
by: Daz | last post by:
Hi everyone. Sorry for the confusing subject, I couldn't think how best to word it. What I would like to know, is if there is an equivilant to this code, in using JSON. <script...
1
by: =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki | last post by:
We have here class of C++ language: namespace Apt { class Name1 { public: class Name2; }; } I 'm give ebove class Name2 so a declalation of class without a definition
65
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.