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?
An
intwould work fine in most cases.That’s what
DateTime.Yearis and that’s what theDateTimeconstructor takes in, so unless you have a specific reason for needing another data type, an integer is probably the easiest thing to work with.