Solved

string data type

Posted on 2004-03-30
7
183 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:fart_boy21
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 10720723
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
 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 10721876
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
 
LVL 4

Assisted Solution

by:Jarodtweiss
Jarodtweiss earned 50 total points
ID: 10722754
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
 
LVL 10

Expert Comment

by:eternal_21
ID: 10724168
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
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10724353
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now