DateTime in .NET

To get the time and date from the system, why do I need System.DateTime.Now? As you can see, the name of the system is already indicated in the namespace. If I just write DateTime.Now, this will not work. I found out earlier that if we declare "use of the system", we do not need to declare or write System.Console.WriteLine or System.DateTime.Now, etc.

using System; using System.Text; namespace DateTime { class Program { static void Main(string[] args) { Console.WriteLine("The current date and time is " + System.DateTime.Now); } } } 
+4
source share
2 answers

This is because your namespace is already called DateTime , which collides with an existing class name. So you can:

 namespace DateTime { using System; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("The current date and time is " + DateTime.Now); } } } 

or find the best naming convention for your own namespaces that I would recommend you do:

 using System; using System.Text; namespace MySuperApplication { class Program { static void Main(string[] args) { Console.WriteLine("The current date and time is " + DateTime.Now); } } } 
+11
source

Because your program class is in a namespace called DateTime. This conflict means that the compiler will look for a type called Now in your DateTime namespace, which obviously does not exist.

Rename the namespace and you won't have a problem.

+3
source

Source: https://habr.com/ru/post/1332610/


All Articles