In C# there are String
objects and string
objects.
What is the difference between the two? What are the best practices regarding which to use?
In C# there are String
objects and string
objects.
What is the difference between the two? What are the best practices regarding which to use?
There is no difference. string (lower case) is just an alias for System.String.
There is not a difference. string is an alias that the compiler converts to System.String.
In fact, it's even aliased in MSIL:
.method private hidebysig static void Main(string[] args) cil managed
There is no difference between them. string is just an alias for System.String. When compiled they both are compiled to System.String object.
The lower case version is just an alias to the actual class String. There is no real difference as far as IL generated.
They are aliases and are interchangeable. However, stylistically, for declarations, I use the lowercased string, and for the static methods, I use String.
string foo = "bar";
if( foo != String.Empty )
{
Console.WriteLine(String.Format("foo.Length = {0}", foo.Length));
}
No difference. System.String
is strictly identical to string
. Common C# coding guidelines indicates that you should use the keyword string
.
One is System.String the .Net Type and one is specific to C# which turns out to be an alias back to System.String.
http://msdn.microsoft.com/en-us/library/362314fe(VS.71).aspx
There is no difference. string is a C# language keyword which refers to the class System.String, just like int is a keyword which refers to System.Int32.
In the future, try compiling an app that uses both and then use Reflector (change the language to IL) to view the compiled output. You'll see there's no difference.
Considering that an “int” is different in some languages depending on 16bit/32bit system, a "string" could in the future evolve to not be the same as System.String.
But for now it is.