Solved

string data type

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Error 2 62
C#.NET and microsoft certification. 3 40
Coding C# in Linux 8 60
How can i use linq for parsing string datetime to real datetime? 16 44
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

19 Experts available now in Live!

Get 1:1 Help Now