'using' statements

G

Guest

Hi,

I sometimes see code where the 'using' statements at the top of the file are
located
before the namespace declaration, and sometimes they are located inside it.
For example:

using System;
using System.Collections;

namespace MyNamespace
{
...
}

and

namespace MyNamespace
{
using System;
using System.Collections;

...
}

What is the diference between the two?

Thanks,
Ben
 
B

Barry Kelly

Ben said:
I sometimes see code where the 'using' statements

These are using declarations, not using statements. Using statements are
for resource protection; using declarations are for importing
namespaces.
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?

If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration. E.g., in a
single file:

---8<---
namespace X
{
using Y; // limits Y to this declaration of X
}

namespace X
{
// members of Y namespace are not in scope here
}
--->8---

I'm not aware of any other differences.

-- Barry
 
J

Jon Skeet [C# MVP]

Barry Kelly said:
These are using declarations, not using statements. Using statements are
for resource protection; using declarations are for importing
namespaces.

They're actually directives, using the terminology of the spec :)
 
B

Barry Kelly

Jon said:
They're actually directives, using the terminology of the spec :)

Yes... I've been living in Delphi-land (+ C) again recently owing to new
job at CodeGear, and Delphi's "directive"s are something else, so that
word has had its meaning nudged over a bit in my little head :)

-- Barry
 
J

Jon Shemitz

Barry said:
I sometimes see code where the 'using' statements
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?

If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration.
I'm not aware of any other differences.

One related difference is that using directives can't 'see' directives
in the same scope, but *can* see directives in an outer scope.

For example,

namespace Outer
{
using Regexen = System.Text.RegularExpressions;
//using Regex = Regexen.Regex; // can't see Regexen, here
namespace Inner
{
using Regex = Regexen.Regex;
}
}

The commented-out alias statement in the Outer namespace wouldn't
compile, because it can't 'see' the alias in its scope. However, the
same directive will compile in the Outer.Inner namepscae, because it
can see the alias.
 
J

Jon Shemitz

Barry said:
I sometimes see code where the 'using' statements
at the top of the file are located
before the namespace declaration, and sometimes they are located inside it.
[...]
What is the diference between the two?

If you have more than one namespace declaration in a file, it limits the
scope of the symbols imported by the using declaration.
I'm not aware of any other differences.

One related difference is that using directives can't 'see' directives
in the same scope, but *can* see directives in an outer scope.

For example,

namespace Outer
{
using Regexen = System.Text.RegularExpressions;
//using Regex = Regexen.Regex; // can't see Regexen, here
namespace Inner
{
using Regex = Regexen.Regex;
}
}

The commented-out alias statement in the Outer namespace wouldn't
compile, because it can't 'see' the alias in its scope. However, the
same directive will compile in the Outer.Inner namespace, because it
can see the alias.
 
B

Brian Gideon

Ben,

There is one more subtle difference that Barry didn't mention.
Consider the following example and notice how the *same* statement
produces a completely different effect. I believe the key part in the
specification that describes this behavior is in section 10.7 (ECMA),
but the wording is very confusing. Maybe someone can verify that?

// The following line imports namespace B types (Bar1 & Bar2).
using B;
namespace A.C.D
{
// The following line imports namespace A.B types (Foo1 & Foo2).
using B;
}

namespace A.B
{
public class Foo1 { }
public class Foo2 { }
}

namespace B
{
public class Bar1 { }
public class Bar2 { }
}

Brian
 
S

Sandeep

I guess this has to do with the VS.
If you use an alias inside the namespace and then use intellisense to update
the reference to other namespaces it will add the new using statements below
your alias.

Regards
Sandeep
 

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