[英]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.