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.

Recommended Answers

All 6 Replies

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

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?

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.