String vs string [duplicate]

2019-01-26 04:10发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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


回答4:

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



回答5:

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


回答6:

There is no difference between them. string is just an alias for System.String. When compiled they both are compiled to System.String object.



回答7:

The lower case version is just an alias to the actual class String. There is no real difference as far as IL generated.



回答8:

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.



回答9:

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.



回答10:

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.



回答11:

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.



回答12:

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.