In C #, are the terms "primitive" and "literal" interchangeable?

Today's discussion led me to the question of whether I understand primates and literals correctly.




My understanding is that a literal type is specifically a type that can have a value assigned using a notation that both a person and a 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 elementary data types that the compiler can understand, for example, int:

 int age = 25; 



... a literal can be non-primitive, such as VB9 support for XML literals. A non-real world example would be if System.Drawing.Point could be assigned 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 a question that, in turn, made me ask the above questions): My understanding is that a type is primitive or literal, there is no direct relation to whether it is a value or a reference type.

For example, System.String is a reference type that supports literals. Custom structures are composite value types that do not support literals.

Is my understanding (if not my explanation) for the most part correct?




Update: Thanks for the great info and conversations! For those who find this, be sure to read the comments, as well as the answers, there will appear some great clarifications, as well as some interesting notes.

btw: this is an ax between which the answer really deserves to get a big green check. I give this, unfortunately, a top-down answer that contains not only a decent answer, but also a lot of clarification and information in the comments. To be fair, there is not a single best answer, there are at least three :)

+12
literals primitive value-type reference-type
Jan 14 '10 at 17:15
source share
8 answers

I assume that one thing that you have not talked about is space and distribution. Primitives are value types and are allocated on the stack (if they are not associated with the object), except for the type of the string, as you mentioned (the string class allocates its space on the heap).

Although the objects themselves contain primitives, they store storage where the actual object that is on the heap is located.

Another thing is that your expression is written quite well. Do you have a specific question that I missed :)?

0
Jan 14 '10 at 17:23
source share

I just wanted to add a short note here.

The C # language specification clearly defines a "literal" - a literal is a representation of the source code of a value. Literals are things like true , 10 , 5.7 , 'c' , "hello" and zero - they are text representing a specific value.

The C # language specification uses the word "primitive" twice; it is never defined and it is completely unclear what this can mean.

The C # language specification does not need to use or define the word "primitive" and therefore should not use this vague term. I spoke with Mads and we agreed that future issues of the specification would be rewritten to completely eliminate this use.

The way type system specifications are - reflection library, CLI, VES, etc. - define the word "primitive", of course, depends on them.

Thanks for raising the question.

+15
Jan 25 '10 at 19:51
source share

As far as I understand (if not my explanation) for the most part?

I disagree on one point: Literal is some compile-time constant (like "Hello World" , 5 or 'A' ). However, there are no "Literal types"; literally always the actual value.

Primitive types are basic IMO types, such as string, int, double, float, short, ...

So primitive have their own types of literals associated with them.

+3
Jan 14 '10 at 17:20
source share

Yes, a literal is a value expressed in the source code, so when VB supports date / time and XML literals, C # does not.

From the C # specification, section 2.4.4:

A literal is a source code representing a value.

As you say, this is not related to the value type of the vs reference type type - the string is really a reference type.

One literal that no one has mentioned null yet, by the way ...

It is also not associated with primitive types - from Type.IsPrimitive :

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

... the C # specification does not actually define the idea of ​​a "primitive" type, but note that String not in the list above.

In terms of literals, which are compile-time constants ... in C #, each literal has a representation that can be baked directly in the assembly; additional literals in VB mean that they are not constants since the CLR understands them - for example, you cannot use const DateTime , but they are still literals.

+3
Jan 14 '10 at 19:24
source share

Here is the MSDN page talking about CLS, which includes a string as a primitive type:

The .NET Framework class library includes types corresponding to the primitive data types that compilers use. Of these types, the following are CLS-compatible: Byte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, IntPtr, and String. For more information about these types, see the type table in the .NET Framework Class Library Overview.

+1
Jan 17 '10 at 3:27
source share

Remember that there is an ASP.Net Literal class .

EDIT: So the answer to the question in the title is missing, as there is no “primitive” class that provides the same functionality. However, this can be seen as a little intellectual answer alec.

0
Jan 14 '10 at 17:55
source share

I think your understanding is basically correct. As winSharp93 said, literals are values ​​that themselves have types, but there is no such thing as a "literal type". That is, although you may have string literals, strings are not a "literal type". As you may have guessed, what defines a literal is that the value is written directly to the source code, although your requirement that the type should not be specified seems too strict (for example, F # has array literals and can infer type array literal [| 1; 2; 3 |] , but does not necessarily infer the literal type of the empty string [| |] ).

Unfortunately, I do not think that there is a well-agreed definition of what makes primitive. Of course, as John Skeet points out, the CLR has its own primitive definition ( Type.IsPrimitive ) that excludes strings. However, other authoritative sources consider string and even object primitive types inside C #. I prefer this definition because C # has built-in support for strings, for example, using the + operator to concatenate and using == as equality of values, not reference equality, and the fact that the string type can be passed using the short form string instead of using the full name of System.String .

0
Jan 14 '10 at 21:50
source share

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

IMHO primitive types can simply be defined as types that directly "exist" in each base platform / host : if you have already played assembly language, you know that you have bytes, words, double words ... but you don’t have lines or decimal places.

Indeed .Net decimal numbers are “ emulated ” by the .Net runtime and are not directly processed by hardware that only understands IEEE 754 floating point numbers (float and doubles, which are then primitive types).

An extension of the concept of literal values "Literal types" can be considered as any type whose values ​​can be directly expressed in a given language (C #, VB.Net, CIL ...). With this definition, literal types will be: all primitive types + strings + decimal numbers .

0
May 6 '14 at 10:00
source share



All Articles