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

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

Example (note the case):

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

What are

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

    The best answer I have ever heard about using the provided type aliases in C# comes from Jeffrey Richter in his book CLR Via C#. Here are his 3 reasons:

    • I've seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.
    • 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 in fact 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
    

    So there you have it. I think these are all really good points. I however, don't find myself using Jeffrey's advice in my own code. Maybe I am too stuck in my C# world but I end up trying to make my code look like the framework code.

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

    string is an alias in C# for System.String.
    So technically, there is no difference. It's like int vs. System.Int32.

    As far as guidelines, it's generally recommended to use string any time you're referring to an object.

    e.g.

    string place = "world";
    

    Likewise, I think it's generally recommended to use String if you need to refer specifically to the class.

    e.g.

    string greet = String.Format("Hello {0}!", place);
    

    This is the style that Microsoft tends to use in their examples.

    It appears that the guidance in this area may have changed, as StyleCop now enforces the use of the C# specific aliases.

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

    Yes, that's no difference between them, just like the bool and Boolean.

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

    I prefer the capitalized .NET types (rather than the aliases) for formatting reasons. The .NET types are colored the same as other object types (the value types are proper objects, after all).

    Conditional and control keywords (like if, switch, and return) are lowercase and colored dark blue (by default). And I would rather not have the disagreement in use and format.

    Consider:

    String someString; 
    string anotherString; 
    
    0 讨论(0)
  • 2020-11-21 05:25

    Against what seems to be common practice among other programmers, I prefer String over string, just to highlight the fact that String is a reference type, as Jon Skeet mentioned.

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

    There is no difference.

    The C# keyword string maps to the .NET type System.String - it is an alias that keeps to the naming conventions of the language.

    Similarly, int maps to System.Int32.

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