简体   繁体   English

.NET中的DateTime

[英]DateTime in .NET

To get the time and date from system why do I need System.DateTime.Now ? 要从系统获取时间和日期,我为什么需要System.DateTime.Now? As you can see there is a System namespace already declared at the top. 如您所见,已在顶部声明了一个System命名空间。 If I just write DateTime.Now it doesn't work. 如果我只是写DateTime.Now它不起作用。 I learned earlier that If we declare "using System" then we don't have to declare or write System.Console.WriteLine or System.DateTime.Now etc. 我之前了解到,如果我们声明“使用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);
        }
    }
}

It's because your namespace is already called DateTime which clashes with an existing class name. 这是因为您的命名空间已经被称为DateTime ,它与现有的类名冲突。 So you could either: 所以你可以:

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 a better naming convention for you own namespaces which is what I would recommend you doing: 或者为你自己的命名空间找到一个更好的命名约定,这是我建议你做的:

using System;
using System.Text;

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

Because your Program class is in a namespace called DateTime. 因为您的Program类位于名为DateTime的命名空间中。 That conflict means the compiler will look for a type called Now in your namespace DateTime which obviously doesn't exist. 这种冲突意味着编译器会在你的命名空间DateTime中寻找一个名为Now的类型,它显然不存在。

Rename your namespace and you won't have the problem. 重命名您的命名空间,您将不会遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM