views:

185

answers:

6

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?

Thanks, Ram

+3  A: 

Both articles says 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 valye 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.

Fedearne
If you see the first article, it says Visual Basic .NET defines the following primitive types: which lists : The String reference type, which represents a sequence of Unicode characters and maps to System.String. The default value of the String type is a null reference.
Ram
@Ram - yet you've tagged this question C#... there are lots of places where VB uses slightly different interpretations. (clarification; the [vb.net] tag was added later)
Marc Gravell
@ Marc - Thanks for pointing it out. I have included VB.NET tag. I just want to know if there is any difference between C# and VB.NET primitive types.
Ram
@Ram - they are **exactly** the same types, so no actual difference. If VB wants to **call** string primitive, then fine. But that doesn't make it a .NET primitive.
Marc Gravell
+1  A: 

.NET defines (from your article):

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

So no. Inbuilt and very important: yes, but not a primitive.

VB uses a slightly different definition to the CLI and C# it seems.

Marc Gravell
+1  A: 

Change-of-stance Update: No since code doesn't lie

Console.WriteLine(typeof(string).IsPrimitive); => False
Console.WriteLine(typeof(int).IsPrimitive); => True

-----end of update.
But some documentation online seems to treat String as a primitive. I think Yes - based on the following definition of "primitive". (My personal definition would be a type which can't be broken down further into component types. But I guess we're just being 'pedantic' here, it's a non-issue for me mostly.)

all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided.

Source: http://msdn.microsoft.com/en-us/library/ms228360%28VS.80%29.aspx Another article in favor - MSDN Mag article

Summary: I guess the answer depends on your definition of primitive, which is not unambiguously defined. Source: Eric Lippert on another SO thread.

Gishu
By your definition I guess only the single bit is primitive? ;-) (Int32 is nothing more than 4 bytes stacked together for instance)
Isak Savo
that definition doesn't mean that all types in `System` namespace are primitives (i can add types to `System` namespace in my assembly, so they will be primitives?), it just states that all exisiting primitive types can be found in `System` and nowhere else.
max
@Isak - you can take any guideline to the extreme :) Getting the 3rd byte out of an Int has no individual/specific use in most cases. Getting the FirstName out of a composed Address type does. So the Int would be a primitive to me.. the address won't.
Gishu
@max - I also included an language defined alias for the type - so String has an alias string. A custom type in the System namespace wouldn't have one AFAIK.
Gishu
@gishu: yeah I know, I was deliberately taking it to the extreme to make a point. I'd say extracting the third byte of an int is about as common as extracting the third character of a string - I've done both occasionally. I'm giving you +1 for your summary because that's spot on - nobody seems to be able to clearly define what *primitive* means
Isak Savo
@Isak - I was going for has the 'no independent significance in the 90% case' angle. While I don't have a good definition for primitive at the moment... :D (Hint: while I do not currently have a scathing retort, you check your email periodically for a doozy. - Sheldon Cooper)
Gishu
A: 

No, the string is not a primitive type.

However, it has some characteristics common with the primitive types.

The language supports string literals in the code, so that you don't have to explicitly create String instances using the new keyword to get a string object.

There is also support for concatenating strings using the + operator, which the compiler turns into a call to the String.Concat method.

Strings are immutable, which means that it in most situations has value type semantics, just like the primitive types.

Guffa
so why isn't it primitive? I mean what characteristic does it *not* have that other primitive types have?
Isak Savo
@Isak Savo: What do yo mean?
Guffa
@guffa: I mean you say it's not primitive but then lists stuff that could argue that it should be called primitive.. You never mentioned why it *isn't* a primitive type. (I'm sincerely interested, and I'm not the one who downvoted)
Isak Savo
@Isak Savo: Primitive types are values that processors can handle natively. The processor has instructions for adding doubles, but not strings.
Guffa
A: 

String is a special primitive type. It is not a value type, but can be considered a primitive type because it can be created by writing literals, eg/ "hello" and it is possible to declare a constant of type string. Having said that, the value of IsPrimitive returns false

Console.WriteLine("hello".GetType().IsPrimitive) // output = False

EDIT: I want to take back my answer here. It is technically not a primitive type, but shares the properties I stated above.

Tim Carter
A: 

In c# the types are primarily defined as two types: value types and primitive types.

First see the definition of primitive types in C#.

On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32 and double is the short form of System.Double.

Now, read this article for the difference: Primitive Types & Value Types

System.String maps to "string", which is a primitive type in the CLI. But in the reality, value types are the ones which go in the stack and not in the heap space.

So, the key is Value types vs Primitive types. By Microsoft's definition of primitive, it is a primitive type, but in a more general sense, its not.

zengr