Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter type for years

Tags:

c#

types

I am writing a method which accepts year as parameter. I.e. four digit number equal or less than current year. Calendar is Gregorian only (for now.. not sure about the future) and I most certainly won't need anything BC.

Which data type am I to use? Obvious solutions would be using DateTime or Int32 :

public void MyFunction(DateTime date)
{
     // year to work with: date.Year;
     // date.Month, date.Day, etc. is irrelevant and will always be
}

or

public void MyFunction(Int year)
{
     if ( year > 9999 || otherValidations == false )
     {
         //throw new Exception...
     }

     // year to work with: new DateTime(year, 1, 1);
}

Any other alternatives apart from writing my own custom data type Year?

like image 218
Sejanus Avatar asked Dec 02 '11 19:12

Sejanus


1 Answers

An int would work fine in most cases.

That's what DateTime.Year is and that's what the DateTime constructor takes in, so unless you have a specific reason for needing another data type, an integer is probably the easiest thing to work with.

like image 68
Brandon Avatar answered Sep 22 '22 12:09

Brandon