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

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

Example (note the case):

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

What are

30条回答
  •  情话喂你
    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.

提交回复
热议问题