I am curious about the string and primitive types. Article like this says string is primitive type. However second article on MSDN does not list string as primitive type.
However when I ran the code provided in second article, it displays String is not Primitive type.
Can any one guide me on this?
' String is a primitive data type: False.
Is String a primitive data type or an Object? A string is an Object that gets special handling in Java. It is not a primitive, but its important enough to the language and programming in general to be treated in some regards like one.
Primitive variables store primitive values. Reference types are any instantiable class as well as arrays: String , Scanner , Random , Die , int[] , String[] , etc. Reference variables store addresses to locations in memory for where the data is stored.
That's what they are called – primitive types. For example, int is a primitive type, but String is a composite type that stores its data as a table of characters (where each character is a primitive type char)."
Both articles say that string is NOT a primitive type. Which it is not.
If you compile and run the example code from the second article it would print:
string is not a primitive type.
I think the confusion about this is, that the syntax of of creating a new string is similar to creating value types.
When defining a value type all of these are equal (on a 32 bit system anyway)
System.Int32 a = new System.Int32(5); System.Int32 a = 5; int a = 5;
Just like these when creating a reference type string:
System.String s = new System.String(new char[]{'h', 'e', 'l', 'l', 'o'}); System.String s = "hello"; string s = "hello";
Also we can compare strings by value even though they are reference types:
s == "hello";//true
This still does not make string a primitive type.
The accepted answer to this question should give you details on that.
There is no "Microsoft" definition of what a primitive type is.
There are only definitions of primitive types in a given context.
System.Boolean
System.Byte
System.SByte
System.Int16
System.UInt16
System.Int32
System.UInt32
System.Int64
System.UInt64
System.IntPtr
System.UIntPtr
System.Char
System.Double
System.Single
System
namespace), a way to define instances of that type with a literal; and permitting the use of these types as constants; the primitive types in VB.NET are: System.Byte
System.SByte
System.UInt16
(UShort
)System.Int16
(Short
)System.UInt32
(UInteger
)System.Int32
(Integer
)System.UInt64
(ULong
)System.Int64
(Long
)System.Single
System.Double
System.Decimal
System.Boolean
System.DateTime
(Date
)System.Char
System.String
The C# specification (version 4) defines keyword aliases for some types, and also defines way of specifying literals for some values; it also defines, separately, which types are available in constant expressions; the closest concept to "primitive types" that C# has is in section 4.1.4: Simple types. (the word "primitive" is only used twice in the 600 pages document); these primitive types are simply defined as "value types that have a keyword alias in C#" - string
is not mentioned in that section:
System.SByte
(sbyte
)System.Byte
(byte
)System.Int16
(short
)System.UInt16
(ushort
)System.Int32
(int
)System.UInt32
(uint
)System.Int64
(long
)System.UInt64
(ulong
)System.Char
(char
)System.Single
(float
)System.Double
(double
)System.Boolean
(bool
)System.Decimal
(decimal
)You will see that there is only a partial overlap between all of these things; the CLR sees both pointer types as primitive, both VB.NET and C# see decimal as a primitive/simple type, only VB.NET sees DateTime as anything special, both VB.NET and C# have a keyword alias and a literal syntax for strings but only VB.NET specifies String
as being a "primitive type", while C# simply has a section of its specification dedicated to System.String
...
In conclusion: different contexts have different definitions for what a "primitive type" is. It does not matter - just learn how to use your programming language, there is no sense in fighting and thinking over such polymorphic words. Personally, I wonder why the property Type.IsPrimitive
even exists.
As for System.String
:
String
is its own very special snowflake;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