Is DateTime a primitive type?
Asked Answered
S

3

10

According to what I can find I believe DateTime is a primitive type but when I check my DateTime variable the property IsPrimitive is false.

7.3 Primitive Types (archive.org mirror)

In the article above you'll see that they say DateTime is primitive. So is there anything I'm doing wrong or have I read the article wrong?

Slenderize answered 5/11, 2014 at 9:31 Comment(6)
"The Date value type is a primitive type, which represents a date and/or a time and maps to System.DateTime"Frechette
This article is a Visual Basic article.Fencible
typeof(DateTime).IsPrimitive returns false as well.Bonina
Why do you care? Being primitive or not is completely irrelevant in practice. DateTime is simply a value type.Judson
This question deserves more credit than it has gotten. It is surprising to not have a direct equivalent of VB.NET's Date in C#, and there's no telling how many people have spent at least a brief moment searching for one.Jessalin
@Judson It's not irrelevant. For example determining whether the [FromURI] or [FromBody] attributes will apply the default behavior depends on whether the value is a primitive type or not in an API MethodYashmak
C
7

The MSDN page on IsPrimitive lists the .net types that are considered as primitive as far as this method is concerned:

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

And DateTime is not in this list.

Convolve answered 5/11, 2014 at 9:35 Comment(0)
C
8

Date (which maps to System.DateTime) is a primitive type of the Visual Basic .NET language (VB.NET for short).

It's not a primitive type in C#, and it's not a primitive type in the CLR either.

A primitive type for a given language is a type for which you can write a string literal, and this literal is understood by the compiler to be of the relevant type. You can't do this for DateTime in C#.

A primitive type for the CLR is a type on which some low level optimizations are allowed. It's very restricted: only string and the different integer and floating-point numbers structs are primitive types.

Cowbind answered 5/11, 2014 at 9:35 Comment(3)
I wouldn't call decimal primitive, despite the compiler magic. I would avoid the word "primitive" when talking about C#, since the spec doesn't define it properly. At the CLR level the concept it at least defined, but still of little practical relevance.Judson
To the CLR, String is not primitive in the sense that typeof(string).IsPrimitive is false.Variety
@Judson I agree! The C# Language Specification mentions predefined struct types which have a big intersection with the types the CLR considers primitive. Although neither set is included in the other. Of course C# also has keywords for the "predefined" types string, object and dynamic, but they are reference types. Finally, the special type void has its own keyword in C#, but C# does not consider it a struct type, apparently, and the CLR does not consider it primitive. void is an anomalous value type.Variety
C
7

The MSDN page on IsPrimitive lists the .net types that are considered as primitive as far as this method is concerned:

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

And DateTime is not in this list.

Convolve answered 5/11, 2014 at 9:35 Comment(0)
T
0

Use Type.IsPrimitive to determine whether a type is a primitive.

For your particular question, you can try DateTime.Now.GetType().IsPrimitive. (This does return false). The link in the accepted answer refers to the Visual Basic language specification primitive types ...

Tailor answered 11/3, 2016 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.