C# Tips and Tricks: Setting default DateTime values

Here is the standard way that I set default values for a DateTime field. I like to set defaults for my DateTime fields whenever it makes sense. In this example I am setting the values so that it will return all the data for the last month, including the current day.

public bool StandardMethodName(DateTime? startDate, DateTime? endDate)
{
     startDate = startDate.GetValueOrDefault(DateTime.Today.AddMonths(-1));
     endDate = endDate.GetValueOrDefault(DateTime.Now);

     return ProcessInfo(startDate, endDate);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s