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 specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations:
var firstName = "John"; // "John" is literal
var firstName = (string)"John"; // *if* the compiler didn't understand that "John"
// was a literal representation of a string then I
// would have to direct it as such
My understanding of primitives is that they are essentially the elemental datatypes which the compiler can understand, such as int:
int age = 25;
...a literal could be non-primitive, such as VB9's support for XML literals. A non-real world example would be if System.Drawing.Point could be assigned with literals:
Point somePoint = 2,2; // both X and Y are primitive values, however Point is a
// composite value comprised of two primitive values
Finally (and this is the question that in turn led me to ask the above questions): My understanding is that whether a type is primitive or literal there is no direct relation to whether it is a Value or Reference type.
For example System.String is a reference type which supports literals. Custom-defined structures are composite value types which do not support literals.
Is my understanding (if not my explanation) correct for the most part?
Update: Thanks for the great info and conversations! To anyone finding this, make sure to read the comments as well as answers, there's some great clarifications spread around as well as a few interesting side-notes.
btw: it's a toss-up between which answer really is deserving to get the big green check. I'm giving it to the unfortunately downvoted answer which contains not only a decent answer but lots of clarification and info in the comments thread. To be fair there isn't one best answer here, there's at least three :)
I just wanted to inject a quick note here.
The C# language specification clearly defines "literal" -- a literal is a source code representation of a value. Literals are things like true
, 10
, 5.7
, 'c'
, "hello"
and null -- they are text that represents a specific value.
The C# language specification uses the word "primitive" twice; it is never defined and completely vague as to what it could possibly mean.
The C# language spec has no need to use or define the word "primitive" and therefore should not make use of this vague term. I've had a talk with Mads and we've agreed that future editions of the spec will be reworded to eliminate this usage completely.
How other type systems specifications -- the reflection library, the CLI, the VES, and so on -- define the word "primitive" is of course up to them.
Thanks for bringing up the question.
Is my understanding (if not my explanation) correct for the most part?
I do not agree in one point:
A literal is some kind of compile time constant (as "Hello World"
, 5
or 'A'
). However, there are no "Literal-Types"; the literal always is the actual value.
Primitive types are IMO "basic" types like string, int, double, float, short, ...
So primitive have their types of literals connected with them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With