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
Related
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.
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.
Using linq to query a datatable returns the following error: CS0117: 'DataSet1.map DataTable' does not contain a definition for 'AsEnumerable'
Project includes reference for System.Data.Datasetextensions.
Here's the code.
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Data.Common;
using System.Data.DataSetExtensions;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
var query1 = from mfg_nm in DataSet1.mapDataTable.AsEnumerable()
select mfg_nm;
}
running it w/out AsEnumerable() results in
var query1 = from mfg_nm in DataSet1.mapDataTable
select mfg_nm;
CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type.
The method you want is in the System.Data namespace, so that using directive is fine, but you also need a reference to the System.Data.DataSetExtensions assembly. Are you sure you've got that reference as an assembly reference?
It's not clear why you've got a using directive for a System.Data.DataSetExtensions namespace - does that not raise an error?
What is the exact error with the AsEnumerable() call? (I'm surprised about the error you're getting with the second form... that's not the error I'd have expected.)
Add System.Data.DataSetExtensions from "nuget" or "add reference"
Add this code:
using System.Data.DataSetExtensions;
In all cases where this happens, the reference to System.Data.DataSetExtensions.dll was missing. If in doubt, try creating a simple console project targeting .NET 4 with a reference to System.Data.DataSetExtensions.dll, to verify that adding the reference actually works.
Also note that you only need to use the System.Data namespace.
BTW mapDataTable is a DataTable, right?
I got this error message:
'System.Data.DataTable' does not contain a definition for 'AsEnumerable' and no extension method 'AsEnumerable' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?)
Added
using System.Data;
Added "System.Data.DataSetExtensions" to the References section. It resolved the problem.
Google search "system.data.datatable does not contain a definition for asenumerable" brought me here, and my trouble was missing:
using System.Data;
Due to my implement the error message was a bit misleading. Hence, my answer to this question. Code was like...
public List<mytype> MyMethod(params) {
return new mynamespace.myclasslib.myclass().GetDataTable(params).AsEnumerable()
.etc
}
Once I tried to explicitly declare the DataTable, it became obvious that I was missing the using statement.
Try this code :
DataSet1.mapDataTable.Select().AsEnumerable()
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.
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).