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

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

Example (note the case):

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

What are

30条回答
  •  无人共我
    2020-11-21 05:27

    string is a reserved word, but String is just a class name. This means that string cannot be used as a variable name by itself.

    If for some reason you wanted a variable called string, you'd see only the first of these compiles:

    StringBuilder String = new StringBuilder();  // compiles
    StringBuilder string = new StringBuilder();  // doesn't compile 
    

    If you really want a variable name called string you can use @ as a prefix:

    StringBuilder @string = new StringBuilder();
    

    Another critical difference: Stack Overflow highlights them differently.

提交回复
热议问题