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.
Short answer: there is no runtime difference — the C# keyword string is an alias for System.String. That point was correctly noted by and , and both forms compile to the same IL and behave identically at runtime.
Practical guidance: prefer the C# keyword string for declarations, parameters and locals because it reads as native C# and cannot be accidentally shadowed. The identifier String is resolved by normal name lookup (affected by using directives and the current namespace) and therefore can be hidden by a user-defined type named String. Use the fully qualified System.String only when clarity is needed (documentation, explicitly showing the framework type) or when name conflicts require it.
A short demonstration of the shadowing difference (compile and run to see the effect):
namespace ShadowTest
{
class String
{
public static System.String Tag(System.String s) { return "[shadow] " + s; }
}
class Demo
{
static void Main()
{
var result = String.Tag("hello"); // resolves to ShadowTest.String
var empty = string.Empty; // always System.String
System.Console.WriteLine(result + " | '" + empty + "'");
}
}
} Notes and best practices: using string avoids having to depend on using System and prevents accidental type shadowing; there is no performance or behavioral advantage to String over string. For consistency with most C# style guides, use the keyword form in everyday code and the System. form only in the rare cases described above.
Jump to Post— TylerSBreton 13What 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 …
Jump to Post— TylerSBreton 13Thanks 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 …
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
both are the same thing. Look to this link
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.