Was playing around a little bit with the TimeSpan class and I started wondering about why the properties of the timespan class are all int32s?
I understand why the constructor takes an int32 when you create your timespan, but if you look at the MSDN documentation it states:
“The hour component of the current TimeSpan structure. The return
value ranges from -23 through 23.”
and that the property looks like this
public int Hours { get; }
My first instinctive thought me was that it was sharing some kind of data with TimeSpan.TotalHours, but that’s a double.
Is there a reason for this being a int32 instead of a int8 that I’ve totally overlooked? or is there some hidden feature in the struct that I’ve missed?
Dealing with 32-bit integers no different to dealing with a smaller precision (such as Int16 or byte) in terms of performance.
So consistency is more important here than saving a few bytes here and there.
Another problem that can rise is when you try to run operations on smaller integers (addition, multiplication) and you risk the overflow or you have to cast to a bigger integer.