有关类和方法的C#常规 [英] C# general on classes and methods
问题描述
System.DateTime currentTime = System.DateTime.Now;
上面的这一行调用System类的now方法.
1.但是,为什么不使用new?
2. Now之后的括号为什么不存在?
我的想法应该是
System.DateTime currentTime = new System.DateTime.Now();
System.DateTime currentTime = System.DateTime.Now;
This line above invokes the now method of the System class.
1. However, new is not used why?
2. the bracket after Now does not exist why?
The way I think, it should be
System.DateTime currentTime = new System.DateTime.Now();
推荐答案
1. However, new is not used why?
因为Now是datetime类的静态成员.
Because Now is a static member of the datetime class.
2. the bracket after Now does not exist why?
现在是属性而不是方法.括号不能与属性一起使用.
一些参考:
-静态(C#参考) [属性(C#编程指南) [
Now is a property not a method. Parenthesis are not used with properties.
Some references:
- static (C# Reference)[^]
- Properties (C# Programming Guide)[^]
现在是静态属性(因此没有()
)会为您提供当前时间.
new DateTime()
不可能,因为构造函数不是公共的.
正如OP指出的那样,DateTime
确实确实有一个公共构造函数,并且还有更多,所以不知道我在想什么.不好意思
Now is a static property(thus no()
) which gives you the current time.
new DateTime()
is not possible because the constructor is not public.
As OP pointed outDateTime
does indeed have a public constructor + alot more, don''t know what I were thinking o_O sorry
您没有阅读语言手册-为什么?
—SA
You don''t read language manual — why?
—SA
这篇关于有关类和方法的C#常规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!