Is String a primitive type?

Ram picture Ram · Oct 19, 2010 · Viewed 54.7k times · Source

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?

Answer

Fedearne picture Fedearne · Oct 19, 2010

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.