Harsh Baid
Harsh Baid

Reputation: 7249

Why does DateTime's AddMonths function takes Int32 parameter and not Int16

I am very curious about this thing

Int32

int addmonths_int = 10;
DateTime.Now.AddMonths(addmonths_int);

Int16

short addmonths_short = 10;
DateTime.Now.AddMonths(addmonths_short);

If we could give Int16 as Parameter in the AddMonths function and also the month's value can never be more than 12 then why do .NET Framework uses the month as Int32 and not Int16...

If is there any culture specific problem in declaring the month as Int16... !!??!!

I am here thinking that if month would have been Int16 then it would saved some bit of length in some where .. i think Memory Allocation

UPDATE

what would be the suggestion for DateTime.Now.Month property couldn't it be Int16 instead of Int32 ??

IS IT ALL ONE AND THE SAME ??

Upvotes: 0

Views: 935

Answers (4)

Chris McAtackney
Chris McAtackney

Reputation: 5222

You can add more than 12 months to a given date using the AddMonths function.

The actual restriction is as follows though;

Months value must be between +/-120000

Upvotes: 1

Simone
Simone

Reputation: 11797

Your assumption is incorrect:

also the month's value can never be more than 12

Even if you were correct, I don't think it would have bought you much using a 16-bit integer instead of a 32-bit one: probably the size of a DateTime object wouldn't change at all.

Upvotes: 1

jason
jason

Reputation: 241583

If we could give Int16 as Parameter in the AddMonths function and also the month's value can never be more than 12 then why do .NET Framework uses the month as Int32 and not Int16...

Why can't you add thirteen months to today and end up with June 25, 2012?

Upvotes: 3

Philippe Leybaert
Philippe Leybaert

Reputation: 171734

First of all, it can be more than 12 months. Nothing stops you from calculating the date plus 435345 months.

As for the Int32 choice: Int32 is the native integer data type of 32 bit systems, so it is the most efficient data type to work with.

Upvotes: 9

Related Questions