Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
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();
Posted
Updated 6-Sep-11 18:48pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Sep-11 1:17am    
1) Why do you think new is needed; 2) Why you think it "should be"? -- no, it shouldn't.
--SA

1. However, new is not used why?

Because Now is a static member of the datetime class.

2. the bracket after Now does not exist why?

Now is a property not a method. Parenthesis are not used with properties.

Some references:
- static (C# Reference)[^]
- Properties (C# Programming Guide)[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Sep-11 23:55pm    
All correct, my 5. I also answered, please see :-)
--SA
Wendelius 7-Sep-11 1:09am    
Thanks SA
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 out DateTime does indeed have a public constructor + alot more, don't know what I were thinking o_O sorry
 
Share this answer
 
v3
Comments
AspDotNetDev 6-Sep-11 18:13pm    
Comment from OP (originally posted as an answer): "the constructor of the DateTime class is public."
Philippe Mori 6-Sep-11 19:16pm    
Half good response --- half points.
Simon Bang Terkildsen 6-Sep-11 21:14pm    
Thank you Philippe, I had probably voted it down due to the extremely incorrect information about the constructor(s) of DateTime
Philippe Mori 6-Sep-11 22:19pm    
Points readjusted!!!
Sergey Alexandrovich Kryukov 6-Sep-11 23:58pm    
Sorry, Simon -- try to forget this answer for a while and read it later using a fresh glance. Will you easily understand what are you talking about? I doubt it. Sorry, no points this time. And Mika provided comprehensive answer already. Thanks for understanding.
--SA
You don't read language manual — why?

—SA
 
Share this answer
 
Comments
Wendelius 7-Sep-11 1:10am    
Now that's a good question, 5.
Sergey Alexandrovich Kryukov 7-Sep-11 1:15am    
:-) Thank you, Mika.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900