A using namespace directive can only be applied to namespaces - c#

using System.Text.RegularExpressions;
using System.DateTime;
DateTime returnedDate = DateTime.Now();
it give me error :
A using namespace directive can only be applied to namespaces;
'System.DateTime' is a type not a namespace (line 1, pos 1)
where is my mistake?

where is my mistake?
It is here: using System.DateTime;
DateTime is a class inside System namespace, not a namespace. In C# you can apply using directive only to namespaces. Adding using XYZ to your program lets you omit the namespace prefix XYZ from classes inside that namespace - for example, to reference class XYZ.ABC you can write ABC. The using directory does not go down to class level, though (this is in contrast to Java's import directories, where .* at the end of the name is optional).
Fix this by replacing using System.DateTime; with using System;
EDIT : (in response to a comment by Karl-Johan Sjögren) There is another using construct in C# that lets you create aliases of types. This construct takes class names, but requires you to specify a new name for them, like this:
using DT = System.DateTime;
Now you can use DT in place of System.DateTime.

using System;
DateTime returnedDate = DateTime.Now();

You should use namespace like this way:
using system;
OR this way with out using namespace:
System.DateTime returnedDate = System.DateTime.Now();

using System;
DateTime returnedDate = DateTime.Now();

DateTime is a type which means its a class.
C# keyword "using" can only be used with namespaces. so in order to use DateTime class in your code , you don't need to write like this.
using System.DateTime;
Rather than writing above line,Simply Include System Namespace like this.
using System;
And use DateTime class in code.

In C# 6 you can do
using static System.DateTime;
var now = Now;

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.

How to get the using statements for a class using EnvDTE?

I am working on a T4 template which produces partial classes based on existing partial classes.
Sometimes the generated code will reference types being used from the existing (non-generated) codebase.
The generated code must either fully qualify these types, or mimic the using statements it finds in the non-generated code.
Mimicking using statements seems better since it will support cases where the type is being referenced from a [Attribute(typeof(Something))], where EnvDTE only returns the string literal "typeof(Something)".
So: how do I find these using statements? I'm using tangible T4's AutomationHelper, but still can't seem to find a solution :(
You can get the using statements by looking at the FileCodeModel.CodeElements for the ProjectItem.
Each ProjectItem has a FileCodeModel property. The FileCodeModel.CodeElements will contain a CodeImport for each using statement. Note that the FileCodeModel.CodeElements will contain other things not just CodeImportss o you will need to check the type returned or filter the unwanted types.
An example is shown below. Here I am using the NuGet's Package Manager Console and PowerShell.
$p = Get-Project
$fileCodeModel = $p.ProjectItems.Item("Class1.cs").FileCodeModel
$fileCodeModel.CodeElements | % { $_.Namespace }
The code above assumes there is a Class1.cs file in the root of the project. For each using statement it will print the full namespace. Note that in the above code it is trying to print the Namespace for each CodeElement and some of the elements will not have this property so you will need to restrict this so it only looks at CodeImport types. The above will work for the following class file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
}
}
If you have using statements between the namespace ClassLibrary1 and the public class Class1 part you will need to do more work and look at the CodeNamespace members since the CodeImports will not be available directly from the FileCodeModel.CodeElements, but hopefully the above code should point you in the right direction.

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

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.

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

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