Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In C#,there are String and string.
What's the difference between them?
A majority of people on the interenet say that they are nearly the same. But in my program, using string is wrong, and using String is right. I just define a constant string.
Posted
Updated 15-May-11 23:36pm
v2

None. There is no difference. string (C# Reference)[^]. The string type represents a sequence of zero or more Unicode characters. string is an alias for String[^] in the .NET Framework.
 
Share this answer
 
v2
Comments
ambarishtv 16-May-11 5:03am    
my5
Kim Togo 16-May-11 5:09am    
Thanks
Dalek Dave 16-May-11 5:23am    
That's it then!
Sergey Alexandrovich Kryukov 16-May-11 5:29am    
True, functionally (my 5), but there is one subtle difference.
Please see my answer.
--SA
flyicing 16-May-11 6:26am    
What you say is MSDN, I have read it!
But still thank you!
There should be NO difference. Please show us the relevant code for better help.
:-)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-11 5:28am    
True, functionally (my 5), but there is one subtle difference.
Please see my answer.
--SA
CPallini 16-May-11 5:42am    
Neither of the two is correct and you should be banned for writing such a code. :-D
flyicing 16-May-11 6:19am    
sorry!
I make a written mistake!
the code is:
string str="aaaa";
String str="aaaa";
CPallini 16-May-11 6:28am    
The following code
//...
int aa=5;
String str1= aa.ToString();
string str2= aa.ToString();
Console.WriteLine(str1);
Console.WriteLine(str2);
//...
works well (as it should) on my system.
Some answers here are correct, some are not.
Right answers are the ones which say there is no difference. You can use System.String anywhere you can use string and visa versa.

More exactly, "string" is a keyword backed by System.String (alias).

Now, I can give you one reason why you may prefer string over String: The code "System.String" is longer, but just "String" may not compile, because you may choose not to add "using System" (I often avoid some using it), but "string" will always work. Same things about "System.Object" vs "object", the later is better.

—SA
 
Share this answer
 
v3
Comments
Abhinav S 16-May-11 5:31am    
I tried
namespace ConsoleApplication1
{
static class Program
{
private const string a = "a";
static void Main(string[] args)
{

}
}
}
and it compiled perfectly. Note: No using system;

Under what circumstances does this not work?
CPallini 16-May-11 5:35am    
Of course that is NOT in contraddiction with Sergey's argument. He said 'String' wouldn't work.
Abhinav S 16-May-11 5:47am    
String does not work.
Sergey Alexandrovich Kryukov 16-May-11 5:50am    
In complete agreement with my answer. Just look at my text again...
--SA
ambarishtv 16-May-11 5:34am    
great!!!
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 16-May-11 5:30am    
Correct. My 5.
However, comprehensive answer is simple -- please see my answer. Also -- can be a preference.
--SA
flyicing 16-May-11 5:30am    
Thank you for your answer!
Depends on how long the string is.
 
Share this answer
 
Comments
CPallini 16-May-11 5:39am    
Life is short, independently by string's lenght...
:-)
flyicing 16-May-11 6:23am    
The length of string?
How to explain this?
Dalek Dave 16-May-11 7:52am    
http://news.bbc.co.uk/1/hi/sci/tech/8363934.stm
AFAIK, string represents working with instances where as String represents the class itself.
If you are using instance variables, you might as well use string.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-11 5:21am    
No! This is exactly the same, only string is a keyword mapped to System.String. Same as object.
--SA
flyicing 16-May-11 6:24am    
Thank you for your answer!
But my string is a constant!
There isn't any defference...
wherever you use string variable = "some string"; at that time actually you are creating an instance of String class. string is immutable so it can't be alter once it create, by means if you want to change any data or any portion of that string, then it will create new object of String class any it will assign to the same reference...

suppose
string s = "good morning"; // it is equivalent to String s = new String("good morning");
s = "good noon"; //here new object will be created of String Class and will be assigned to reference variable named s and old object will no longer referred by any reference variable so garabase collector will remove it.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-May-11 5:21am    
No! This is exactly the same, only string is a keyword mapped to System.String. Same as object.
--SA
flyicing 16-May-11 6:25am    
Thank you for your answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900