[英]DateTime in .NET
要从系统获取时间和日期,我为什么需要System.DateTime.Now? 如您所见,已在顶部声明了一个System命名空间。 如果我只是写DateTime.Now它不起作用。 我之前了解到,如果我们声明“使用System”,那么我们就不必声明或编写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);
}
}
}
因为您的Program类位于名为DateTime的命名空间中。 这种冲突意味着编译器会在你的命名空间DateTime中寻找一个名为Now的类型,它显然不存在。
重命名您的命名空间,您将不会遇到问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.