Search code examples
c#.netvisual-studiovisual-studio-2017coding-style

Why does Visual Studio code style settings think DateTime is not a built-in type but Guid acts like it is?


I have it set so I have to use the explicit type for built-in and apparent types, and var everywhere else:

enter image description here

This works fine for int and even Guid but not for DateTime:

enter image description here

Why does it want to use var for DateTime variables instead of the explicit DateTime type?


Solution

  • As per the docs, DateTime is not a built in type (see also here).

    Only lower case aliases like int or string are built in.

    Your Guid example may work because the "type is apparent".

    That may explain why:

    Guid h = Guid.NewGuid();
    Guid i = h;
    

    h is left alone, but i is not (since NewGuid is classed as a creation method).

    I believe it doesn't work for DateTime.Now since it is not a static method (it is a property). But it does work for DateTime.Parse - for example.