Should 'using' be inside the namespace or outside? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Should Usings be inside or outside the namespace
Are there any technical reasons for preferring this
namespace Foo
{
using System;
using System.IO;
instead of the default
using System;
using System.IO;
namespace Foo
{

Eric Lippert explains this.
In general, they're identical.
However, using statements in the namespace can see namespaces and aliases included outside the namespace.

Almost* the only difference between the two would be if you used more than one namespace in the same file (or if you used the same namespace more than once). I'm not sure why would you do that, bu you certainly can:
using System;
namespace FooNamespace
{
using System.IO;
class Foo
{
// you can use types from System and System.IO directly here
}
}
namespace BarNamespace
{
class Bar
{
// you can't use types from System.IO directly here
// but you can use types from System
}
}
* See SLaks' answer.

No technical reason, just a preference. of course the second chunk of code looks cleaner, though.

Related

using directive is unnecessary visual studio - random class

I want to use Random class but I got this error message:
using directive is unnecessary visual studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Random; //I got this error at this line
namespace Mikrotik
{
....
Quick-fix:
You only need using System; - inside your code you can use Random directly.
Background:
Please read the documentation for C#'s using directive: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive
I think you're imagining that using works like how import works in Java where you have to name each type you want to bring into scope or use .* to bring all types into scope. Instead, C#'s using only imports namespaces, making it roughly equivalent to Java's import being used exclusively with the .* syntax.
You're getting the error because there is no namespace System.Random, instead it's a type-name; so you just need using System; - in your code you can use Random as-is.
For pendantry: C#'s using directive has 3 different modes:
using namespace; - brings into scope all types in the specified namespace
using Alias = namespace.TypeName; - brings into scope a single type, but with an alias. The alias can match the original name.
using static namespace.TypeName; - brings only the static members of the specified type into scope.
Not to be confused with C#s using() {} statement, which is completely unrelated to importing types and namespaces.

Place usings inside or outside of namespace [duplicate]

This question already has answers here:
Should 'using' directives be inside or outside the namespace in C#?
(13 answers)
Closed 9 years ago.
I was learning MVC WebAPI and I was following a tutorial and everything was going fine untill I saw the following:
namespace HelloWebAPI.Controllers
{
using System;
public class ProductsController : ApiController
{
}
}
What we usually do is that we add the resources\scope in the beginning like this:
using System;
namespace HelloWebAPI.Controllers
{
public class ProductsController : ApiController
{
}
}
I want to have a better understanding about it
Thanks.
There is a difference, small but there is.
It's all about the sequence of name resolution made by compiler.
The good answer on subject you can find here:
Should Usings be inside or outside the namespace
In practise in first case compiler, in case could not find a type information immediately,
would search among namespaces declared inside using. In second case, instead, would search first among the actual namespace and after only go to search inside declared outside.
You can define more than one namespace in a C# file.
Putting using statements inside of a namespace means they are only in use within that namespace for that file.
Putting them outside of the namespace means they apply for all namespaces within the file.
It's kind of how the scope of variable names applies only in the most inner braces that contains them and deeper.
The only difference is with scope of using statements. If you use using inside a namespace then these using statements will be included in all files which place under that namespace. And if you use using statements outside namespace then these using statements will be valid only for current file.
File 1:
namespace MyNamespace
{
using System;
using System.IO;
public MyClass
{
}
}
File 2:
namespace MyNamespace
{
public MyClassV2
{
}
}
In this example you don't need to add using in File 2 for MyClassV2 as MyNamespace already has these using statements. But for a different namespace you need to add using statements.

namespaces as result of the entity framework generated code

I have two Reason classes:
1. One that generated by the edmx file and inherited by the object context.
2. One that I created as POCO object.
While I write my queries I need to write the full namespace of the Reason POCO class:
using System.Collections.Generic;
using System.Linq;
using MyProj.Domain.Business.EntitiesRepository.System.Calls;
namespace MyProj.Data.EF4.EntitiesRepository.System.Calls
{
public class ReasonRepository:
EFRepository<MyProj.Domain.Business.Entities.System.Calls.Reason>, IReasonRepository
{
public IList<MyProj.Domain.Business.Entities.System.Calls.Reason> GetReasonsList()
{
return GetQuery().ToList();
}
}
}
If I am not writing the full namespace the compiler consider Reason as the generated object and not as the POCO object I need.
Is there any way of preventing write the full namespace?..
You could use using aliases .. see the example 1 in http://msdn.microsoft.com/en-us/library/sf0df423(v=vs.80).aspx.
How you do it -
in your using directive do something like -
using POCOObjects = MyProj.Domain.Business.Entities.System.Calls
after that you just need to type POCOObjects.Reason
Unless your Reason class is in the MyProj.Data.EF4.EntitiesRepository.System.Calls namespace, I think you can just add using MyProj.Domain.Business.Entities.System.Calls
Otherwise you might want to check out this Q&A C#: Problem trying to resolve a class when two namespaces are similar. The workaround being that you use the global:: namespace alias. More on that here: http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

"using" statement locations within namespace declarations [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Should Usings be inside or outside the namespace
I'm supporting some code that, unusually, has all its using statements contained within the namespace declaration. It makes me a bit uncomfortable but I have no idea if this should be anything to be concerned about or not. I can't think of an immediate problem other than it being counter to usual convention.
Is there anything wrong with:
namespace myProject.controls
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
Instead of the more widely-used:
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace myProject.controls
{
There's nothing to be concerned with here; in fact, the first example (with the using statements inside the namespace declaration) is recommended by default by StyleCop.
The only difference has to do with scoping. In the first case, the using declarations are scoped within the namespace declaration, and are not available outside it, in other namespace declarations in the same file, for example. In the second case, the declarations are in scope for the whole file.
Location of the using statement
affects the scope of the references
within the file.
I cannot think of any situation that would 'warrant' limiting it withing a smaller scope. Though, I would prefer sticking to conventions - or commenting exhaustively - , just to make sure 'next person' does not have to guess my intentions.
Probably this link will answers your question in detail
C# - Location of using statements

Namespaces and Using Directives

If I have a namespace like:
namespace MyApp.Providers
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
}
Does this mean that if I create other files and classes with the same namespace, the using statements are shared, and I don't need to include them again?
If yes, isn't this a bit of a management headache?
No, it's only good for the namespace section inside the file. Not for all files inside the namespace.
If you put the using statement outside the namespace, then it applies to the entire file regardless of namespace.
It will also search the Usings inside the namespace first, before going to the outer scope.
You need to specify the using directive for any classes that you want to reference without qualification in each file where you want to use them.
Reference:
The scope of a using directive is
limited to the file in which it
appears.
No, it doesn't, and so no, it isn't.
Consider the fact that outside the namespace declaration, you are in the global namespace. Do using statements in that region of the source file affect the global namespace in other source files?
No. You'll need to include the namespaces in every class except on partial classes.
One side note: you're doing a very good practice of putting the using statements inside the Namespace. That's very good syntax.
Keep up the good work.
The using statements are valid for the code file in which they appear, with a minor twist; if you put the using statements inside the namespace, they are limited to the scope of that namespace, but still only within the same code file.
Usings only apply to the current file. Whether they're inside or outside the namespace declaration makes only a small difference:
The lookup order for types is as follows:
start in the innermost namespace declaration
look in the current namespace
look in the usings of the current namespace
go up to the parent namespace declaration and repeat from step 2
As a result, this program will compile fine:
namespace MyProject.Main {
using System;
class Program {
public static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
}
// in another file:
namespace MyProject.Console {
class Test {}
}
But if you move the using System; to the top, then the compilation will fail (MyProject.Console.WriteLine doesn't exist).

Categories

Resources