string data type

hi,

Is string a primitive data type in C#? If it is not, why it is a keyword? it comes out in bold when you enter "string" in the editor. In Java, it is part of java.lang.String and not a keyword.(it has a capital S)


thanks.
fart_boy21Asked:
Who is Participating?
 
eternal_21Connect With a Mentor Commented:
Actually strings are not "primitive types" in C#.  The unified type structure in the .NET platform disposes of the idea of primitive types.  In other languages (Java, for example) primitive types are treated differently than class objects.  In .NET, all objects are treated the same, as they all derive from System.Object.

"Now," you say, "not ALL objects are treated entirely the same."  That is true.  Instances of classes that derive from the System.ValueType are stored on the stack, while Instances of classes that do not are stored on the heap with a pointer allocated on the stack.  The 13 value types that are defined in the System namespace are 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).  In C#, these 13 types are called the "Simple Types" and they are as close to the "Primitive Types" as you are going to get.  In fact, what we actually have in C# is the equivalence of "Primitive Types" to "Value Type."

"Now," you ask, "if string is not a primitive type, what is so special about it that the IDE makes it bold, and the C# compiler recognizes this keyword?"  Well, let me tell you.  The 13 types I mentioned earlier are grouped together with System.Object (object), System.String (string) and the Null Type (null) are are part of a set called called the "Predefined Types."

The reason for making System.String a predefined type is so that we can work with literal strings in our code.  In C#, literal objects are always represented by one of the following Predefined Types: System.Bool (literals: true, false), System.Int32, System.UInt32, System.Int64, System.UInt64, System.Single, System.Double, System.Decimal, System.Char, System.String, and the Null Type (literal: null).
0
 
Timbo87Commented:
Yes, strings are primitive types in C#, but they are passed by reference like the object type. In .NET languages, all primitives can be thought of as classes, since they correspond to .NET equivalent classes. (for example, int to System.Int32). For this reason, primitives have methods associated with them, unlike Java. Here is a code sample that would work in C#, but not in Java.

C# - Works:
int x = 5;
Console.WriteLine(x.ToString());

Java - Does not work because there are no methods associated with int:
int x = 5;
System.out.println(x.toString());
0
 
JarodtweissConnect With a Mentor Commented:
A further precision to "eternal_21"

He has clearly explained the different elementary data type. But the difference between the strings and others, is that String is a class, but the single one having a specific behavior.
When you are comparing two classes instances (using the Equals operator), the system will compare the references and not the object itself.
And String is the single object for which the Equals method has been overriden and will compare the string values instead of the string references.
0
 
eternal_21Commented:
Excellent point, Jarodtweiss!

Although the fact that the Equals operator is overriden is not in *itself* significant.  Other .NET System classes offer this functionality (for example, System.DateTime), and you can implement this feature in your own classes.   How about the fact that System.String is immutable?  That is, once an instance of the object has been created, it cannot be modified - you can only create a new string (System.Text.StringBuilder was designed to allow for this), of course System.DateTime is the same way, so I don't know how relative this is either.
0
 
JarodtweissCommented:
eternal_21, there is a difference between String and DateTime beacause DateTime is also an elementary data type, inheriting (inplicitely) from ValueType, but String is the only elementary data type which is a class.
It is the only point I wanted to focus on.
It is a class but act as it was a normal elementary data type (or so...)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.