?
Solved

string data type

Posted on 2004-03-30
7
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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 150 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 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