Link to home
Start Free TrialLog in
Avatar of fart_boy21
fart_boy21

asked on

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.
Avatar of Timbo87
Timbo87

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());
ASKER CERTIFIED SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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...)