Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A using namespace directive can only be applied to namespaces

Tags:

c#

.net

datetime

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?

like image 966
Alex Avatar asked Jul 17 '13 10:07

Alex


4 Answers

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.

like image 155
Sergey Kalinichenko Avatar answered Oct 04 '22 14:10

Sergey Kalinichenko


using System; 

DateTime returnedDate = DateTime.Now();
like image 35
Praveen Prasannan Avatar answered Oct 04 '22 15:10

Praveen Prasannan


You should use namespace like this way:

using system;

OR this way with out using namespace:

System.DateTime returnedDate = System.DateTime.Now();
like image 21
Santosh Panda Avatar answered Oct 04 '22 16:10

Santosh Panda


using System; 

 DateTime returnedDate = DateTime.Now();
like image 33
James Avatar answered Oct 04 '22 14:10

James