Question

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?

Was it helpful?

Solution

There is no difference. string (lower case) is just an alias for System.String.

OTHER TIPS

No difference. System.String is strictly identical to string. Common C# coding guidelines indicates that you should use the keyword string.

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));
}

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 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.

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.

There is no difference because string is converted to System.String by the compiler. Same with all of the common types (int goes to System.Int32, etc). We use the simple name so they stand out.

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.

just a bit note: string/String is not the only couple of aliases: eg. Integer,Int32, int are all aliases.

@mliesen: it doesn't happend in C#, it's not like C. this because from C# you don't create executable but a per-compiled code, as java.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top