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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With