Solved

string data type

Posted on 2004-03-30
7
184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
IO.FileSystemWatcher dies when used in Powershell 1 45
Iterate a dictionnary to change values 4 53
Counting connections to SQL Server through C# 3 29
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

13 Experts available now in Live!

Get 1:1 Help Now