What is the difference between string and String?
If I want
string name = "Joe";
String name = "Joe";
which is the correct usage? Is there a difference?
Thanks in advance.
Well, "string" is actually an object type of the System.String class. So if you omit "using System" from your program, this will not compile:
String a = "Hello World";
Whereas this line of code would compile whether or not "using System" is in your code or not.
string a = "Hello World";
Think of string as a "shortcut" to the System.String class whereas String is basically directly making a variable of type System.String when "using System" is in the program.
(string == System.String)
Reguards,
Tyler S. Breton
TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
Thanks for replying.
So basically, I can use either or as long as I pay attention to whether using System. Using System is automatically added into new projects. I wonder, when would you not using System?
There's no advantage then of using String over string or is there?
There is no advantage. string == System.String.
Reguards,
Tyler S. Breton
TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.
As far as guidelines, I think it's generally recommended to use string any time you're referring to an object. e.g.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474