Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:07:46+00:00 2026-05-26T18:07:46+00:00

Let’s say I am creating an instance of a class object that has both

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:07:47+00:00Added an answer on May 26, 2026 at 6:07 pm

    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.

    • MiscUtil

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I create an object like this: Person: NSString *name; NSString *phone; NSString
Let's say that I have classes like this: public class Parent { public int
Let's say I'm creating an OpenGL game in C++ that will have many objects
Let's say that I have a date in R and it's formatted as follows.
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have some text as follows: do this, do that, then this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.