Q. What is the difference between using "String" and "string" ("Object" vs "object" etc.)?

Asked by Aaron Clauson. Answered by the Wonk on February 21, 2003

A.

An object of type "String" in C# is an object of type "System.String", and it's bound that way by the compiler if you use a "using System" directive, like so:

using System;
...
String s = "Hi";
Console.WriteLine(s);

If you were to remove the "using System" statement, I'd have to write the code more explicitly, like so:

System.String s = "Hi";
System.Console.WriteLine(s);

On the other hand, if you use the "string" type in C#, you could skip the "using System" directive and the namespace prefix:

string s = "Hi";
System.Console.WriteLine(s);

The reason that this works and the reason that "object", "int", etc in C# all work is because they're language-specific aliases to underlying .NET Framework types. Most languages have their own aliases that serve as a short-cut and a bridge to the .NET types that existing programmers in those languages understand.

Personally, I prefer to use the aliases instead of the .NET versions because, as an old-time C/C++ programmer, I prefer to type "int" instead of "Int32" etc. However, both are exactly equivalent, so pick the one you like best and stick with it.

Feedback

I have feedback on this Ask The Wonk answer