要从系统获取时间和日期,为什么我需要 System.DateTime.Now? 如您所见,顶部已经声明了一个系统命名空间。如果我只写 DateTime.Now 是行不通的。我之前了解到,如果我们声明“使用系统”,那么我们不必声明或编写 System.Console.WriteLine 或 System.DateTime.Now 等。
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);
}
}
}
最佳答案
这是因为您的命名空间已被称为 DateTime
,它与现有的类名冲突。所以你可以:
namespace DateTime
{
using System;
using System.Text;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The current date and time is " + DateTime.Now);
}
}
}
或者为您自己的命名空间找到一个更好的命名约定,我建议您这样做:
using System;
using System.Text;
namespace MySuperApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The current date and time is " + DateTime.Now);
}
}
}
关于c# - .NET 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479034/