9

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?

6
  • "The Date value type is a primitive type, which represents a date and/or a time and maps to System.DateTime"
    – devqon
    Nov 5, 2014 at 9:33
  • 3
    This article is a Visual Basic article. Nov 5, 2014 at 9:34
  • typeof(DateTime).IsPrimitive returns false as well. Nov 5, 2014 at 9:46
  • 3
    Why do you care? Being primitive or not is completely irrelevant in practice. DateTime is simply a value type. Nov 5, 2014 at 9:46
  • 1
    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. Jan 3, 2017 at 19:27

3 Answers 3

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.

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. Nov 5, 2014 at 9:47
  • 1
    To the CLR, String is not primitive in the sense that typeof(string).IsPrimitive is false. Sep 16, 2015 at 11:49
  • @CodesInChaos 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. Sep 16, 2015 at 11:55
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.

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 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.