2

System.TimeSpan class only has one non-static private field and that is

internal long _ticks;

So it only keeps ticks and performs all operations (Add, Subtract, TotalSeconds ...) and overloads operators (>=, >, ==) based on that private field, _ticks.

I see that there was no exception methods in .NET 1.0 but TimeSpan was already there but cannot it be redesigned as an alias of Int64 and other members implemented as extension methods? This way the memory footprint would be less right?

(Same applies for Url class?) Any thoughts?

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214

2 Answers2

3

Well, for one thing there is no equivalent of TYPEDEF in C#, so it would make Timespans incredibly difficult to work with.

TimeSpan provides a whole bunch of useful properties and methods, like .TotalHours that wouldn't make sense on an Int64, so simply making it an alias of Int64 wouldn't make much sense.

Also, I'm not entirely certain that there would be a significant (if any) difference in the class's footprint. TimeSpan is a struct, which means it will probably be implemented to have roughly the same footprint as its component pieces (in this case an Int64). (Can anybody help me find a reference to back up this impression?)

Besides that, saying that a TimeSpan is exactly the same thing as a long is just unintuitive and wrong. It increases the chance of error, where people pass a long into a parameter that should be a TimeSpan, and vice versa. Conceptually, it really goes against the grain of object-oriented programming.

I'm sure if I thought long enough about it, I could think of half a dozen other reasons (like preventing future improvements on the TimeSpan class that involve the introduction of another field, etc.), but these are probably more than sufficient.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • But there is some sort of aliasing, right? Like long <--> int64, string <--> String that compiler handles? – ahmet alp balkan Feb 11 '14 at 23:39
  • 1
    @ahmetalpbalkan: Yeah, but that wouldn't give you access to properties like `.TotalHours`, since there's no such property on an Int64. – StriplingWarrior Feb 11 '14 at 23:45
  • @ahmetalpbalkan: each of the compilers has a small set of synonyms, but they are not typedef aliases and you cannot define your own. I suggested this as a new feature early on, but it was hard to come up with good reasons why. – david.pfx Feb 28 '14 at 14:06
  • @david.pfx: I imagine we'll be able to do more "language-tweaking" with Roslyn. It'll probably be horribly abused by some people. :-) – StriplingWarrior Feb 28 '14 at 21:07
  • @StriplingWarrior: Do you think? I don't really get Roslyn yet, but I had never thought it was an opportunity to change the fundamentals. Maybe I should look deeper. – david.pfx Mar 01 '14 at 10:46
  • @david.pfx: There's a lot to Roslyn, and I haven't fully wrapped my head around it either, but I think I remember an example that actually tweaks the language slightly. Adding a new alias keyword, for example, probably wouldn't be very difficult. – StriplingWarrior Mar 02 '14 at 17:42
2

Well, C# doesn't have typedef's, though you can alias a type, but that's irrelevant. If TimeSpan were simply an Int64, then what, they should create a static TimeSpanHelper class with all of the methods and properties that TimeSpan exposes?

That seems silly when you can just create a type which actually functions as a time span. A type which can override operators to allow for fluid and intuitive usage. A type which, you know, fits in well with C#'s type system. What you propose is more akin to C than C#.

Ed S.
  • 122,712
  • 22
  • 185
  • 265