In C# are the terms “Primitive” and “Literal” interchangeable?

前端 未结 8 736
[愿得一人]
[愿得一人] 2021-02-07 20:43

A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct.


My understanding is that a literal type is spec

8条回答
  •  误落风尘
    2021-02-07 21:38

    Just to add that there is another type that blurs the limit: System.Decimal whose values can be expressed as literals in the C# language, but which is not a .Net primitive type.

    IMHO primitive types could be simply defined as types that directly "exists" in every underlying platform/host: if you've already played with assembly language you know that you have bytes, words, double-words... but you have no strings or decimals.

    Indeed .Net decimals are "emulated" by the .Net runtime and are not directly handled by the hardware which only understands IEEE 754 floating point numbers (float and doubles which are then primitive types).

    By extension of the notion of literal values "Literal types" could be considered as any type whose values can be directly expressed in a given language (C#, VB.Net, CIL...). With this definition literal types would be: all the primitive types + strings + decimals.

提交回复
热议问题