Gaijinhunter
Gaijinhunter

Reputation: 14685

Best practice: new DateTime() vs DateTime.Parse()

Let's say I am creating an instance of a class object that has both a name and date. Which is considered best practice when setting the date?

var employees = new List<Employee>() 
{ 
    new Employee {Name = "Foo", HireDate = new DateTime(2000, 1, 15)}, 
    new Employee {Name = "Bar", HireDate = new DateTime(2001, 5, 25)}, 
}; 

or

var employees = new List<Employee>() 
{ 
    new Employee {Name = "Foo", HireDate = DateTime.Parse("2000, 1, 15")}, 
    new Employee {Name = "Bar", HireDate = DateTime.Parse("2001, 5, 25")}, 
}; 

I am assuming there really isn't a huge difference, but I am slightly new to C# so I am not sure which is prefered by the majority of C# programmers and why (performance, readability, etc.). Thanks in advance for any insight you can give!

Upvotes: 8

Views: 3854

Answers (3)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

Prefer new DateTime.

In favor of new, it will allow you to use variables directly:

// clean because the variable is applied directly to the function
int year = 2000;
var date1 = new DateTime(year, 1, 15);
var date1 = new DateTime(year, 7, 3);
var date1 = new DateTime(year, 8, 19);

// kind of gross and prone to errors because it is appended to the string
int day = 23;
var date1 = DateTime.Parse("2001, 5, " + day.ToString());

In argument against Parse, it doesn't detect errors until runtime:

var date1 = new DateTime(200fdsa, 1, 15); // Error on compile
var date2 = DateTime.Parse("2001fdsjf, 5, 25"); // Must run to find the error

DateTime.Parse will always have poorer performance, because it needs to run code that detect errors and converts the string to numeric values. It does this while your program is running.

However - in cases where you must accept string input (e.g. from a text file), you should use the tool built for the job: DateTime.Parse.


If you're interested in simplifying the specification of date/time constants in your code, there is a library that Jon Skeet wrote for that.

It is a bit old and unmaintained, but the specific code (extension methods on integer constants/date and time structures) probably doesn't really need a lot of maintenance.

The source code for those extensions is under MiscUtil\MiscUtil\Extensions\TimeRelated\ (inside the source zip)

It will let you write your date times in a friendlier fashion, yet still give you clean code with similar performance to new DateTime(2012, 11, 13):

DateTime someDateAndTime = 19.June(1976) + 8.Hours();

Upvotes: 14

Daniel Mann
Daniel Mann

Reputation: 59016

I'd just use new DateTime() in the example you provided. You know exactly what the year/month/day components of the date are. Why create a string and parse it?

Upvotes: 4

Kirk Woll
Kirk Woll

Reputation: 77556

I suppose this is ultimately subjective, but I cannot conceive of any good reason for using Parse in this situation. Why perform an (inefficient) string parse over a simple constructor that takes the actual component values?

Upvotes: 6

Related Questions