954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string and String

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.

foo
Newbie Poster
8 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
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?

foo
Newbie Poster
8 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
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
 

both are the same thing. Look to this link Click Here

VatooVatoo
Light Poster
44 posts since Jan 2007
Reputation Points: 20
Solved Threads: 2
 

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
 

I think primitives like int and string as keywords opposed to classes were added to keep C++ programmers happy and porting code less tedious.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

Post: Markdown Syntax: Formatting Help
You