What is the difference between String and string in C#?

后端 未结 30 3914
走了就别回头了
走了就别回头了 2020-11-21 04:35

Example (note the case):

string s = \"Hello world!\";
String s = \"Hello world!\";

What are

相关标签:
30条回答
  • 2020-11-21 05:29

    I'd just like to add this to lfousts answer, from Ritchers book:

    The C# language specification states, “As a matter of style, use of the keyword is favored over use of the complete system type name.” I disagree with the language specification; I prefer to use the FCL type names and completely avoid the primitive type names. In fact, I wish that compilers didn’t even offer the primitive type names and forced developers to use the FCL type names instead. Here are my reasons:

    • I’ve seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used. Similarly, I’ve heard some developers say that int represents a 32-bit integer when the application is running on a 32-bit OS and that it represents a 64-bit integer when the application is running on a 64-bit OS. This statement is absolutely false: in C#, an int always maps to System.Int32, and therefore it represents a 32-bit integer regardless of the OS the code is running on. If programmers would use Int32 in their code, then this potential confusion is also eliminated.

    • In C#, long maps to System.Int64, but in a different programming language, long could map to an Int16 or Int32. In fact, C++/CLI does treat long as an Int32. Someone reading source code in one language could easily misinterpret the code’s intention if he or she were used to programming in a different programming language. In fact, most languages won’t even treat long as a keyword and won’t compile code that uses it.

    • The FCL has many methods that have type names as part of their method names. For example, the BinaryReader type offers methods such as ReadBoolean, ReadInt32, ReadSingle, and so on, and the System.Convert type offers methods such as ToBoolean, ToInt32, ToSingle, and so on. Although it’s legal to write the following code, the line with float feels very unnatural to me, and it’s not obvious that the line is correct:

      BinaryReader br = new BinaryReader(...);
      float val = br.ReadSingle(); // OK, but feels unnatural
      Single val = br.ReadSingle(); // OK and feels good
      
    • Many programmers that use C# exclusively tend to forget that other programming languages can be used against the CLR, and because of this, C#-isms creep into the class library code. For example, Microsoft’s FCL is almost exclusively written in C# and developers on the FCL team have now introduced methods into the library such as Array’s GetLongLength, which returns an Int64 value that is a long in C# but not in other languages (like C++/CLI). Another example is System.Linq.Enumerable’s LongCount method.

    I didn't get his opinion before I read the complete paragraph.

    0 讨论(0)
  • 2020-11-21 05:29

    string is a keyword, and you can't use string as an identifier.

    String is not a keyword, and you can use it as an identifier:

    Example

    string String = "I am a string";
    

    The keyword string is an alias for System.String aside from the keyword issue, the two are exactly equivalent.

     typeof(string) == typeof(String) == typeof(System.String)
    
    0 讨论(0)
  • 2020-11-21 05:30

    Lower case string is an alias for System.String. They are the same in C#.

    There's a debate over whether you should use the System types (System.Int32, System.String, etc.) types or the C# aliases (int, string, etc). I personally believe you should use the C# aliases, but that's just my personal preference.

    0 讨论(0)
  • 2020-11-21 05:31

    string is just an alias for System.String. The compiler will treat them identically.

    The only practical difference is the syntax highlighting as you mention, and that you have to write using System if you use String.

    0 讨论(0)
  • 2020-11-21 05:32

    Using System types makes it easier to port between C# and VB.Net, if you are into that sort of thing.

    0 讨论(0)
  • 2020-11-21 05:36

    C# is a language which is used together with the CLR.

    string is a type in C#.

    System.String is a type in the CLR.

    When you use C# together with the CLR string will be mapped to System.String.

    Theoretically, you could implement a C#-compiler that generated Java bytecode. A sensible implementation of this compiler would probably map string to java.lang.String in order to interoperate with the Java runtime library.

    0 讨论(0)
提交回复
热议问题