9

Most common datatypes have a built in type: Int32 has int, Boolean has bool, String has string, etc. Why is there no built-in type for DateTime?

First I thought it's because DateTime has properties and public functions, but so does int. Can anyone shed some light on this? What's the criteria for a type to have a built-in equivalent?

MeanGreen
  • 3,098
  • 5
  • 37
  • 63
  • 3
    What value would that add? All `double`, `long`, etc. are are _aliases_ for `System.Double`, `System.Int64`, etc. Not all types with aliases are "built-in" types since `decimal` is NOT a primitive type in the CLR. – D Stanley Nov 17 '16 at 14:22

2 Answers2

8

The CLR only defines basic building blocks: the minimal data types necessary to define all others. Those are the types given an alias.

Since DateTime is just a collection of longs and integers, packed in a struct, there is no need to create a new data type in the CLR for it. You can build it with the data types already defined in the CLR. No need for aliasing it.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    I think the OP means: why can we type `int` for `System.Int32` and `string` for `System.String`, but not `datetime` for `System.DateTime`? – Peter B Nov 17 '16 at 14:23
  • 4
    If that is the question, is it quite simple: `DateTime` is not part of the CLR. Hence it has no alias. It is no a primitive type. – Patrick Hofman Nov 17 '16 at 14:26
  • 2
    @PatrickHofman that is not a great argument. `System.Decimal` is not part of the CLR either but we have `decimal`. – Scott Chamberlain Nov 17 '16 at 14:31
  • Well, `decimal` needs to be there [to be CLS-compliant](http://stackoverflow.com/a/13471993/993547). @ScottChamberlain – Patrick Hofman Nov 17 '16 at 14:31
  • is there any link where to read about this, just wondering – Harry Nov 17 '16 at 14:38
  • 2
    This answer by Eric Lippert is by far the best I have found: http://stackoverflow.com/a/10063923/1220550 – Peter B Nov 17 '16 at 14:39
  • C# is not equivalent to the CLR. Can you explain more for those who might not know much why the perfectly sensible minimal type system of the CLR means that C# does have a datetime type? – curiousdannii Nov 17 '16 at 15:03
1

These are just aliases in the language. DateTime doesn't have an alias. That's it.

UPDATE:
According to the C# language specification:

C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the System namespace, as described in the table below.

    Reserved word   Aliased type
    ----------------------------
    sbyte           System.SByte
    byte            System.Byte
    short           System.Int16
    ushort          System.UInt16
    int             System.Int32
    uint            System.UInt32
    long            System.Int64
    ulong           System.UInt64
    char            System.Char
    float           System.Single
    double          System.Double
    bool            System.Boolean
    decimal         System.Decimal

The C# language specification can be found here:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Specifications\1033\CSharp Language Specification.docx

Mitulát báti
  • 2,086
  • 5
  • 23
  • 37